PostgreSQL refuse connection - postgresql

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?

Related

pgpool-II Session to Watchdog Delegate Ip Terminated When Either Primary or Standby Node Failed

I'm trying to setup postgres cluster of two nodes (primary and standby). In order to activate automatic failover, I'm using pgpool-II.
I followed the following article: https://www.pgpool.net/docs/41/en/html/example-cluster.html
and the only thing difference I did is installing postgresql version 12 instead of version 11.
Knowing that I'm trying it useing two centos7 images on VMware. I faced the following issues:
When I run systemctl status pgpool.service on both nodes, it returned success.
Also I can access postgresql using the watchdog delegate IP.
But what testing failover, everything goes wrong.
Scenario 1:
I accessed my database using watchdog delegate Ip.
I disconnect the standby server.
Result: My session to postgresql continued to work for less than a minute and then it failed. and I'm unable to connect again, until I reconnect the standby node, and restart the pgpool service again.
Scenario 2:
I accessed my database using watchdog delegate Ip.
I disconnect the primary server.
Result: My session stopped directly. and the standby server is not promoted to be master.
I noticed something (might be related to the above described problem): when I try to run the following command
psql 192.168.220.146 -p 9999 -U postgres -c "show pool_nodes"
it fails to work and returned the following:
psql: error: could not connect to server: 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.9999"
However if I ran: psql 192.168.220.160 -p 5432 -U postgres
it works fine and I can access the postgres shell.
My pool_hba file:
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all pgpool 0.0.0.0/0 scram-sha-256
host all postgres 0.0.0.0/0 scram-sha-256
Any help would be appreciated.
I followed the following article: https://www.pgpool.net/docs/41/en/html/example-cluster.html and the only thing difference I did is installing postgresql version 11.
I not ping delegate_IP = '192.168.1.233'. May i help you?
Thanks you.
you are not providing -h argument to psql for specifying the IP address. So effectively psql is trying to connect to UNIX domain socket and considering the IP address in the command as the database name.
Try putting -h before the IP address
psql -h 192.168.220.146 -p 9999 -U postgres -c "show pool_nodes"

Cannot allow external connections in Postgres

I have a server with Postgres installed and I want to allow the connection from external IP's. Postgres works very well for local connections, then at the end of the file /var/lib/pgsql/data/postgressql.conf I added:
listen_addresses = '*'
And in the end of /var/pgsql/data/pg_hba.conf I added:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Then I restarted postgres with: sudo systemctl restart postgresql
And tried to connect with: psql -h my.db.server.ip -U postgres
And I get:
psql: could not connect to server: No existe ninguna ruta hasta el «host»
Is the server running on host "my.db.server.ip and accepting
TCP/IP connections on port 5432?
I am using Centos 7 and PostgreSQL 9.2.23
What else should I do?
Use
netstat -an | grep 5432
on the server to see if anything is listening on port 5432.
See what port is set to in postgresql.conf.
Use lsof on the PostgreSQL server process to see what sockets PostgreSQL is listening on.

PSQL: SSL error: unknown protocol

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

Amazon EC2: PostgreSQL remote connection hangs

I'm trying to set up PostgreSQL remote connection on Ubuntu server.
I changed listen_addresses = 'localhost' to listen_addresses = '*' in /etc/postgresql/9.3/main/postgresql.conf
Then I added to postgre config file /etc/postgresql/9.3/main/pg_hba.conf lines
host all all 0.0.0.0/0 md5
host all all ::0/0 md5
host all all all md5
But when I'm trying to connect remote DB by psql -U db_user -h host -d db_name it hangs and show message
psql: could not connect to server: Connection timed out
Is the server running on host "host_address" and accepting
TCP/IP connections on port 5432?
PgAdmin connection hangs as well
Had the same issue using amazon EC2 service
Solution:
1) Go to security group for your server instance
2) Edit inbound rules
3) Allow all traffic for your IP

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.