How to connect to a remote PostgreSQL server using pgAdmin and Unix Domain Socket over SSH tunneling - postgresql

I want to connect to a remote PostgreSQL server from my Windows machine using pgAdmin.
Can I connect to the server using unix domain socket over SSH tunneling?
The PostgreSQL server and the tunneling SSH server are running on the same machine.
In pgAdmin documentation, the Host name/address field of the Server Dialog is explained as:
Specify the IP address of the server host, or the fully qualified domain name in the Host name/address field. If you provide a unix domain socket, the directory name must begin with a “/”.
so, I specified /var/run/postgresql in that field to use a unix domain socket, but the following error message appears:
Unable to connect to server:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
This is the server machine's log:
systemd[1]: Started Session 28 of user ***.
sshd[630]: pam_unix(sshd:session): session opened for user *** by (uid=0)
sshd[634]: error: connect_to /var/run/postgresql: unknown host (Name or service not known)
sshd[630]: pam_unix(sshd:session): session closed for user ***
Of course, I can connect to the PostgreSQL server using a unix domain socket on the server machine.
The following psql command works.
$ psql -h /var/run/postgresql -U *** -d ***

Related

Postgresql not starting even after editing rc file

I receive the following error when I try to start postgresql aka psql in Slackware Linux:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
I am trying to start it and changed the field in pg_hba.conf to trust from md5.
At some point it was asking for a password.
I get the following when I try to start the server:
bash-5.1# postgres -D /pgsql/data
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
bash-5.1# postgres -D /pgsql/data >logfile 2>&1 &
[1] 8310

Problem to connect to an AWS postgres instance via a bastion host via SSHTunnel in python

I have an AWS postgres instance and an AWS ec2 instance (running as bastion host). From my local machine I want to connect to that AWS postgres instance via a python script. But it's not working (see below).
My setup is:
with SSHTunnelForwarder(
('internet-adress-of-the-basion-host', 22),
ssh_private_key=r"the-private-key.pem",
ssh_username="ec2-user",
local_bind_address=('localhost', 6543),
remote_bind_address=('internet-adress-of-the-postgres-server', 5432)) as server:
server.start()
print("server connected")
self.conpostgres = psycopg2.connect(host='localhost', port=server.local_bind_port, database="the-database",
user="the-user", password="the-password")
I can sucessfully connect via the sshtunnel (the "server connected" messages is printed to the console), but I get an ERROR in the when excecuting psycopg2.connect.
The connect ERROR is:
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 6543?
FATAL: password authentication failed for user "the-user"
FATAL: no pg_hba.conf entry for host "x.x.x.x", user "the-user", database "the-database", SSL off
Two remarks:
1.) In the line "no pg_hba.conf entry for host "x.x.x.x"". Here the "x.x.x.x" address is NOT localhost (127.0.0.1). It's a normal ip4 internet address. But I have no idea where this address is coming from!
2.) !!!! With the exact same configuration mentioned above I can successfully connect to the database via the database tool built in IntelliJ Idea. So there is a way to connect to the postgres server without changing pg_hba.conf. So maybe I have to configure the tunnel differently?

Postgres connection error in ubuntu and centos

I am new to postgres. While connecting through ubuntu and centos I'm getting this error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Give permission to all host by editing ipv4 entry in pg_hba.conf file.
After modification restart postgresql service.

Postgres login via localhost timeout

I've installed Postgres.app and modified the port number to 5466, I've enabled listen_addresses='*'.
I've setup 'trust' in pg_hba.conf.
I can connect through the socket via psql. However, psql -h localhost -p 5466 fails and returns *"server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request."
Port 5466 is mapped to postgresql.
Any help as to why I can connect to the socket, but not to localhost would be appreciated. If i connect on another port an error is returned instantly telling me there is no server there, but when I connect on the correct port the connection hangs and returns the error above.
The problem was a program to monitor internet traffic blocked all ports other than 80: https://github.com/PostgresApp/PostgresApp/issues/169

Copy file or directory from remote server

I try to copy file from remote server to my local machin but it gives following error
ssh: connect to host 103.241.144.137 port 22: Connection refused.
command : scp root#111.111.111:/home/msecondo/public_html/jsp/afterLogin/sachin/PHR/ /localpth/.
"Connection refused" means that there was no process accepting connections at the IP address and port that your client tried to connect to. In this case, it probably means that there is no SSH server running at 103.241.144.137 port 22. Alternately, if the SSH server is running, it may not be listening on the IP address or port that you tried to connect to.
StackOverflow isn't the right site for troubleshooting SSH server configuration. If you have administrator privileges on the remote server, then you should look into how the SSH server is configured and whether it's running. If you're not an administrator on the remote server, you should report this to the administrators so that it can be investigated.