Avoid cancel of postgresql script when losing connection to server due to VPN session closing - postgresql

I'm trying to execute a remote postgresql script by command line (connecting to the server via putty) by using
\i myscript.sql
The problem comes when the script takes several hours to complete and VPN connection to the server (and consequently putty session) close. I can't really control that part because it's a local security policy. The session closing makes the execution of the script to cancel.
Is there an easy way to let the script run in the server regardless I'm still connected to it?

After you putty in use GNU screen or one of its equivalents like tmux before you run psql. This will preserve your session when you get disconnected so you just reconnect to it next time you log in.

You can reduce the TCP keepalive idle time on your client operating system to have the client send “keepalive packets” regularly, which keeps the misconfigured firewall from closing the connection.
Start psql using the connection string syntax:
psql 'host=... port=5432 user=... dbname=... keepalives_idle=60s'

Related

VsCode dropping ssh connections frequently with remote server

I'm facing a weird problem after connecting with the remote host using ssh in VSCode, the editor lost the connection after some seconds with the remote server (in this case a droplet in Digital Ocean). But when I open an ssh connection with the remote server using my local terminal instead of my local VSCode of my I do not see the same thing happen, in the terminal the connection keep alive for minutes, as expected.
Perhaps an ssh config on a remote server? I don't think so because using the terminal the problem doesn't occur.
So, what fix I can apply to my local VSCode configuration to stop this behaviour?

How can I transfer a dumped database in Postgres CLI from my remote server to my remote server or my local machine?

Normally, when I transfer a dumped database from postgres CLI in my local machine, I can simply sftp my_user_name#my_ip remotely from postgres CLI to my local machine without any problems. However, when it comes to my remote server, this seems like I have already connected with my server via ssh connection; thus, when I sudo su - postgres to open up postgres CLI, I cannot use the same technique to
i)sftp my_user_name#my_ip. It said request Connection timed out; I think this may be because they are not in the same WiFI network connection.
ii) or even sftp ubuntu#ec2-xx-xx-xx-xxx.....amazonaws.com. It said my aws ec2 server: Permission denied (public key)
I think this may result from I have made a SSH connection already, so if i sftp again, this will duplicate with the previous SSH connection.
Any idea ?? Please help
it should be simple. I will help you here. Firstly are both machines on same network ? your first comment looks like they are not on same network or we can say they are not connected to each other hence connection timed out. on your second comment; u would need to use pem file to connect to ec2.. Please give all details ; and I will give you commands etc. needed. Also, check security group of destination server if port 22 is open.

how to solve the error of DBeaver database?

I used DBeaver (database management tools) and faced this type of error
It looks like you're trying to connect to a MySQL instance over SSH. Most likely issue is that the remote server isn't accepting SSH connections. If you try the following at a command line, can you establish a connection? (Bash if you're using Linux, putty if you're on Windows)
ssh <user>#<remotehost>
Also it's important to mention that you probably have two sets of credentials you're using here. The credentials to connect to the SSH server are not necessarily the same as the credentials to connect to the MySQL database.

Using multiple terminals on a remote machine using a single SSH connection

I'm running a process on a remote server through SSH on a certain port. The process is basically a TCP server waiting (listening) for a connection. So as long as it is running I cannot use the terminal as it is a blocking application.
I want to run another application (a TCP client) through the same instance of SSH connection to connect to that TCP server. I used screen to detach the first process (TCP Server) and connect to the server but then I could not see the output of the TCP server application.
Is there a way to emulate two terminals on a remote machine using SSH?
So I found a solution to this problem. Basically below is a link that explains how can I achieve exactly what I needed. That is, opening multiple terminals using a single SSH connection.
http://idnotfound.wordpress.com/2008/01/14/multiple-terminals-in-a-single-ssh-session/
In short we can use the following procedure:
$ ssh -X mylogin#remotemachine gnome-terminal
Password: ...
[Ctrl+Z]
$ bg
$ exit
Press Ctrl+Shift+N to open a new terminal.
EDIT: Or just use screen. Sigh..

Console pgAdmin-like software

I like the pgAdmin III GUI software, but the GUI uses more bandwidth than SSH console.
psql is not interactive, without menus, tables list, etc.
Does exist some interactive text-mode tool to connect to PostgreSQL ?
There is no text-mode window-and-menu curses/ncurses style text mode interface for PostgreSQL; no ncurses equivalent to PgAdmin-III.
I strongly recommend learning psql and getting comfortable with it. You could use PgAdmin-III remotely as detailed below, but in the long run you'll be massively more productive if you learn psql.
Use PgAdmin-III via ssh tunnels or direct connection
You can always connect with PgAdmin-III via an ssh tunnel or remote TCP/IP connection. That way you aren't transmitting all the GUI data over the network, just the PostgreSQL protocol data.
For ssh, do something like:
ssh -L 15432:localhost:5432 remote_host
then while the ssh session is open, connect to localhost port 15432 to make a connection to the remote DB.
This will work even if the remote DB is only listening on 127.0.0.1. It'll also work if you ssh into a bastion host then connect to the DB server from there; just change localhost in the -L argument to the IP/hostname of the Pg server. For more information see the ssh manual, particularly LocalForward for IP forwarding, ProxyCommand for custom multilayer tunnels, and the -D option for dynamic SOCKS proxying.
It's even possible to use an ssh tunnel to connect to a server that's only listening for unix socket connections, by running socat to proxy between the unix socket and TCP.
None of this will work when you're connecting to a Windows host, but rdp2tcp can be used to tunnel TCP over RDP connections for similar effect. See this question.
Use psql
psql is pretty interactive. Though it isn't a GUI windowing interface, it's hardly just a scripting tool. It provides lots of visibility into the system with the \d commands, lots of info via \h, tab completion, paging, \e break-out command editing, and lots of other interactive features.
Use \? for psql help, and \h SQL_COMMAND for syntax of a particular SQL command, eg \h INSERT.