PSQL: SSL error: unknown protocol - postgresql

all. I have a new Ubuntu 17.04 server setup. I can access it through ssh successfully. Also, I have a postgresql database installed. It is already configured to accept remote connections through the following configurations:
pg_hba.conf
host all all 0.0.0.0/0 md5
postgresql.conf
listen_addresses = '*'
But when I try to access the database from my computer, I get the following error:
psql -h [SERVER_IP] -d db_production -U dbuser
psql: SSL error: unknown protocol
expected authentication request from server, but received S
I also have a docker container running inside the server. If I access it through /bin/bash and also try to connect with the server db, I get this error:
psql -h [SERVER_IP] -d db_production -U dbuser
psql: could not connect to server: Connection refused
Is the server running on host "[SERVER_IP]" and accepting
TCP/IP connections on port 5432?
The port 5432 is opened and the firewall is disabled. Does anybody have gone through that?

First, do you use the local server host when you're connected into the docker? The server IP, in that case, should be localhost or 127.0.0.1.
Try to use the complete DB URL:
psql postgres://dbuser:dbPass#SERVER_IP:5432/dbname?sslmode=prefer

Related

server refuses connection after enabling ssl on Postgres 14 server on ubuntu 20.04

I am following a couple of online post to enable ssl on Postgres 14 server on ubuntu 20.04. Generate CA and server keys, setup postgresql.conf and pg_hba.conf, also stop and start the Postgres server. show ssl is on.
However sudo -U postgres and psql -h mydomain.com throws error:
psql: error: connection to server at "mydomain.com" (xxx.xxx.xxx.141), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
It seems that the problem is with pg_hba.conf. What I did is to add the following line at the end of the file, without changing anything else:
hostssl all all 0.0.0.0/0 scram-sha-256 clientcert=verify-full

PostgreSQL refuse connection

I'm deploying two servers with two PostgreSQL databases (one Primary and one Standby model). I have set the pg_hba.conf on my primary server to
host replication replicator <my standby ip addr> md5
and I've also enable the listen_address settings in postgresql.conf
listen_addresses = '*'
But when I was trying to run this command on my standby server
pg_basebackup -h <primary server ip addr> -D /var/lib/postgresql/12/main -U replicator -P -v -R -X stream -C -S node2
it returned this error message
pg_basebackup: error: could not connect to server: Connection refused
Is the server running on host "<primary ip addr>" and accepting
TCP/IP connections on port 5432
It is also verified that access to port 5432 has been enabled since I am able to connect to it from pgadmin via SSH.
my server is running on Ubuntu 20.04 with Postgresql version 12 installed
Is there anything I'm missing?

PostgreSQL: Remotely connecting to Postgres instance using psql command

I want to remotely connect to a Postgres instance. I know we can do this using the psql command passing the hostname
I tried the following:
psql -U postgres -p 5432 -h hostname
I modified the /etc/postgresql/9.3/main/pg_hba.conf file on the target machine to allow remote connections by default
I added the following line to the file
host all all source_ip/32 trust
I restarted the cluster using
pg_ctlcluster 9.2 mycluster stop
pg_ctlcluster 9.2 mycluster start
However, when I try to connect from the source_ip, I still get the error
Is the server running on host "" and accepting TCP/IP connections on port 5432?
What am I doing wrong here?
I resolved this issue using below options:
Whitelist your DB host from your network team to make sure you have access to remote host
Install postgreSQL version 4 or above
Run below command:
psql -h <REMOTE HOST> -p <REMOTE PORT> -U <DB_USER> <DB_NAME>
psql -h <IP_Address> -p <port_no> -d <database_name> -U <DB_username> -W
-W option will prompt for password. For example:
psql -h 192.168.1.50 -p 5432 -d testdb -U testuser -W
I figured it out.
Had to set listen_addresses='*' in postgresql.conf to allow for incoming connections from any ip / all ip
Step Wise below
Opening the Port - Make sure the PSQL Port is open to all remote connections or connections from a specific set of IPs as per your requirement. PSQL, in general, runs at port 5432, and it is configurable, so expose relevant Port accordingly.
Update Remote Server PSQL Configuration - Set listen_addresses = '*' in postgresql.conf file, path in general is /etc/postgresql/psql_version/main/postgresql.conf
Connect remotely - psql -U <db_username> -h <IP_address> - in case psql is running on a port other than 5432 on the remote server, specify port by adding -p <port_number>
A little plus below -
In case the IP has been mapped to a domain name, you can connect by replacing <IP_address> with <host_name>. To do this, add a new connection rule in pg_hba.conf file
Note -
All above explained can cause security issues - best practice always is to either keep your psql port closed, or only allow a list of IPs to connect through the port.
Note that "ident" in pg_hba.conf requires a "ident server" to be running on the client.

pgAdmin3 backup over ssh tunnel

I have a running postgresql server on amazone ec2. I connect with pgAdmin3 to it over ssh tunnel directly configured in pgAdmin3 from my mac.
I can make queries and see the full schema, no problem about that.
If I try to make a backup of the database (from pgAdmin3 GUI) then I get (even if the connection is actually open and working) the following exception:
/Applications/pgAdmin3.app/Contents/SharedSupport/pg_dump --host localhost --port 5432 --username "MY_USERNAME" --role "MY_ROLE" --no-password --format custom --encoding UTF8 --verbose --file "/Users/XXX/filename" "DATABASENAME"
pg_dump: [archiver (db)] connection to database "DATABASENAME" failed: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Process ended with Exitcode 1.
Any idea why pg_dump in the background can not connect over the ssh tunnel?
alternative until i found a solution is to make it by terminal
ssh <HOST> "pg_dump -U <USERNAME> -W -h localhost -F c <DATABASENAME> | gzip -c" > ./backup.sql.gz
This line worked for me:
ssh -o "Compression=no" server_adress "pg_dump -Z9 -Fc -U postgres db_name" > backup_name.dump

Postgresql: Unable to connect through psql at console to default localhost

Postgresql server running and verified on 5432 on my localhost system:
If I type: psql -l I get the following response:
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"?
If I type psql -h localhost -l, it works and gives me a list of the databases.
The pg_hba.conf file is wide open, showing:
TYPE DATABASE USER ADDRESS METHOD
The value "local" is for Unix domain socket connections only:
local all all trust
Allow any IP to connect without password:
host all all 0.0.0.0/0 trust
IPv4 local connections:
host all all 127.0.0.1/32 trust
IPv6 local connections:
host all all ::1/128 trust
What have I missed? On other systems the first call from the command line works fine.
It sounds like when you are running the command you are connecting to localhost, not the file socket.. try
psql -h localhost -p 5432
Default Admin Login sudo -u postgres psql
Login into specific db with privilages psql -h host -p port -U User_Name db_name
Is the server running locally and accepting connections on Unix domain
socket "/var/run/postgresql/.s.PGSQL.5432"?
This just means that the unix_socket_directory configuration parameter on the server differs from the default of the client-side psql.
/var/run/postgresql is the default Unix domain socket path for Debian-based packages. For a self-compiled server, it is /tmp. It may also be a custom path specified in postgresql.conf or through a start directive.
Assuming it's /tmp you could do psql -l -h /tmp. The command knows that the parameter following -h is to be interpreted as a directory and not as a hostname because it starts with a slash.