Connection to remote postgresql host running docker refused - postgresql

I have an instance of postgresql running in a docker container.
I can connect to the database from the host that is running docker by:
docker exec -u root -it postgres bash
And then accessing the database from there by doing an su to user postgres.
If I use a client from a desktop pc / laptop to try and connect I get a connection refused:
psql -h 20.XXX.1XX.1XX -p 5432 -U <user>
psql: could not connect to server: Connection refused
Is the server running on host "20.XXX.1XX.1XX" and accepting
TCP/IP connections on port 5432?
I have edited the pg_hba.conf file in the docker instance and added the following:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
If I run netstat, again within the container I get:
root#ee9dg39913cdc:/# netstat -na | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 52651 /var/run/postgresql/.s.PGSQL.5432
And when I run it on the machine hosting the docker instance:
root#VM01:~# netstat -na | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
I do not have ufw running at all, so, no firewall issues. The host is an Azure VM and port 5432 is open to the internet.
postgresql.conf is set as:
listen_addresses = '*'
Given all of the above, can anyone help me understand why I cannot connect to the postgres instance over the internet using:
psql -h 20.XXX.1XX.1XX -p 5432 -U <user>
Thanks.

Related

No response from postgres server

I'm trying to set up a small postgresql server on my raspberry pi. So far, I've:
Installed postgres onto my pi
Set a static IP adress for my pi
edited /etc/postgresql/13/main/postgresql.conf so that listen_addresses = '*'
edited /etc/postgresql/13/main/pg_hba.conf to include
host all all 0.0.0.0/0 md5
Running pg_isready on my pi gives me
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
However, running the following results in no response
root#pibox:~# pg_isready -d <db_name> -h<static_ip> -p 5432 -U postgres
<pi's IP>:5432 - no response
Additionally, I'm not able to connect from my laptop
psql --host=<static IP> --port=5432 --dbname <db name> --username=postgres
psql: error: connection to server at "<the pi's IP>", port 5432 failed: Connection refused
What are some of the next steps I can take so that I can connect to my postgres server from my laptop and execute queries against the databases?
EDIT:
My laptop is running macOS 12.4. I'm connecting to my pi via ssh over my home network. The pi is running a copy of DietPi. Here is some more relevant info
root#pibox:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
Running the following commands yeilds these outputs
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
root#pibox:~# pg_isready -d postgres -h <Pi IP> -p 5432 -U postgres
<Pi IP>:5432 - no response
root#pibox:~# pg_isready -d postgres -h 127.0.0.1 -p 5432 -U postgres
127.0.0.1:5432 - no response
root#pibox:~# pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log

Unable to connect to postgres sql on Kubernetes minikube from Client tool

I have successfully deployed postgres on Kubernetes.
postgres NodePort 10.96.66.202 <none> 5432:30030/TCP
I am able to connect to postgres using localhost and 5432 with the following command:
kubectl exec -it postgres-75b8fd84f-gkj6k -- psql -h localhost -U appuser --password -p 5432 appdb
But when I tried to access the psql using another client tool using the node port and minikube IP I am getting the below error:
host=$(minikube ip)
192.168.49.2
port=$(kubectl get service postgres -o jsonpath='{.spec.ports[0].nodePort}')
30030
$ psql -h 10.96.66.202 -U appuser --password -p 30030 appdb
Password:
psql: error: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "10.96.66.202" and accepting
TCP/IP connections on port 30030?
Edit:
I tried the minikube IP as well:
$ psql -h 192.168.49.2 -U appuser --password -p 30030 appdb
Password:
psql: error: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "192.168.49.2" and accepting
TCP/IP connections on port 30030?
I also tried this combination as well:
$ psql -h 10.96.66.202 -U appuser --password -p 5432 appdb
Password:
psql: error: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "10.96.66.202" and accepting
TCP/IP connections on port 5432?
The IP 192.168.49.2 is for the node, and 10.96.66.202 is for the service.
And the nodePort 30030 is for the node, and the targetPort 5432 is for the service.
So you can use either
psql -h 192.168.49.2 -U appuser --password -p 30030 appdb
or
psql -h 10.96.66.202 -U appuser --password -p 5432 appdb

access postgres into docker container when postgres locally is also running

I have postgres on docker container that I run using the following command:
docker run -d --name timescaledb -p 127.0.0.1:5433:5433 -e POSTGRES_PASSWORD=somepass timescale/timescaledb:latest-pg12
Meanwhile I also have postgres installed locally in my machine and port 5432 is listening:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 124 postgres 7u IPv6 0x38eef62419af50a3 0t0 TCP *:5432 (LISTEN)
postgres 124 postgres 8u IPv4 0x38eef624199e6d5b 0t0 TCP *:5432 (LISTEN)
com.docke 3520 alex 39u IPv4 0x38eef62428c6b7fb 0t0 TCP localhost:5433 (LISTEN)
com.docke 3520 alex 42u IPv4 0x38eef62428c3581b 0t0 TCP *:49816 (LISTEN)
I'm trying to set up a connection with psycopg2 using this command:
connection = psycopg2.connect(host=config.DB_HOST,database=config.DB_NAME,user=config.DB_USER,password=config.DB_PASS,port=config.DB_PORT)
of which config.xxx is :
DB_HOST = 'localhost'
DB_USER = 'postgres'
DB_PASS = 'somepass'
DB_NAME = 'etfdb'
DB_PORT = '5433'
but I'm getting the following error:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5433 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
It's worth noting that the command docker ps shows me port 5432 for some reason I don't know:
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d2623bd4055 timescale/timescaledb:latest-pg12 "docker-entrypoint.s…" 6 days ago Up 17 minutes 5432/tcp, 127.0.0.1:5433->5433/tcp timescaledb

Postgres in Docker port matching

I need to run several equals Postgres containers on different ports via docker-compose. My problem is that I do not understand how to connect from my terminal to container's psql.
First of all, I should say that my local Postgres is off, so 5432 is free. Also I have listen_addresses = '*' in my containers.
Here is list of my attempts (to make it easier, I've posted examples for one Postgres container):
Run without port specifying
docker-compose.yml:
version: "3"
services:
postgres:
build:
context: ../../
dockerfile: db-load-test/sharding-benchmark/Dockerfile
environment:
- POSTGRES_PASSWORD=postgres
check port:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
try to connect:
$ psql -p 5432 -U postgres
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"?
$ psql -h localhost -p 5432 -U postgres
psql: 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?
$ psql -h 0.0.0.0 -p 5432 -U postgres
psql: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Specify port and expose it in .yaml (I've changed 5432 to 5433 to make it more expressive)
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp, 0.0.0.0:49162->5433/tcp,:::49162->5433/tcp
Similar attempts to connect was unsuccessful. Also it's unclear to my why Postgres still listen 5432:
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-08-02 12:49:01.397 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Use docker directly
docker run --rm -it -e POSTGRES_PASSWORD=postgres -p 5432:5432/tcp postgres:11
It works with psql -h localhost -p 5432 -U postgres, but if I use docker run with 5432:5433 it breaks, which is unclear to me.
In your docker-compose.yml file, you need to specify ports: for each of the database containers. In each of these the second number must be the normal port for the database (5432); the first number can be any number of your choosing that's not used by another container or host process.
version: '3'
services:
pg1:
image: postgres
# Use default PostgreSQL port 5432 on the host
ports: ['5432:5432']
pg2:
image: postgres
# First port must be different from pg1's above
# Second port must be exactly 5432
ports: ['5433:5432']
When you connect from the host, use a host name of localhost and the first published ports: number. (If you're using the older Docker Toolbox setup, use the docker-machine ip address of the Docker VM; if you're connecting from a different host, use the Docker host's DNS name or IP address.)
# connect to pg1
psql -h localhost -p 5432 -U postgres
# connect to pg2
psql -h localhost -p 5433 -U postgres
You do not need to expose: ports in the docker-compose.yml file; this doesn't really actually do anything. You shouldn't need to reconfigure the stock postgres image beyond its basic environment-variable settings. It doesn't make sense to connect to 0.0.0.0, a special address that means "everywhere" when you're setting up a network listener.
If you're connecting from a different container, none of this matters. Use the Compose service name of the other container (like pg2) and the standard port number (5432). ports: are completely ignored, and localhost means "this container". The two containers must be on the same Docker network; Compose will create a network named default and just using that without explicit networks: settings is fine.

Why psql doing nothing with -h param?

I have installed postgres, then I created new user and opened md5 authentication on 127.0.0.1 in pg_hba.conf
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
But now if I try to connect via psql with "-h" flag with "127.0.0.1" and this user psql doing nothing.
-bash-4.2$ psql -h 127.0.0.1 -U k4fntr
it is just freez
The problem was with iptables. I discovered that I had no something about postgres in iptables.
This command had fixed the problem
iptables -I INPUT -p tcp -m tcp -s 127.0.0.1 --dport 5432 -j ACCEPT