Docker and Postgres - Cannot connect to container - postgresql

I am attempting to start PostgreSQL in a docker container, but cannot connect from my terminal.
After running docker-compose up -d using the following docker-compose:
docker-compose.yaml
version: '3.8'
services:
db:
image: postgres:14.5
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5416:5432'
volumes:
- ./api/db/postgres-test-data:/var/lib/postgresql/data
Running docker ps gives me:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8611fc5a9a73 postgres:14.5 "docker-entrypoint.s…" 21 seconds ago Up 12 seconds 0.0.0.0:5416->5432/tcp, :::5416->5432/tcp api_db_1
But when I try to connect with psql -h localhost -p 5416 -U postgres, I get:
psql: error: connection to server at "localhost" (::1), port 5416 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
How can I fix this?

It was because I had my VPN running. Disabling it fixed the issue.

Related

Can not connect docker containered postgres db

docker-compose.yml:
services:
db:
image: mdillon/postgis:11
restart: always
ports:
- "5465:5432"
volumes:
- ./data/postgisdb:/var/lib/postgresql/data
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=p#ssword
- POSTGRES_DB=db_test
Then I run docker-compose up -d --build --remove-orphans
docker ps shows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7d734c1f23c mdillon/postgis:11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5465->5432/tcp, :::5465->5432/tcp app_db_1
When I try to connect database via pgadmin with parameters
Host name/address: localhost
Port: 5465
Maintence database: db_test
Username: user
Password: p#ssword
I still get error password authentication failed for user "user".
What am I doing wrong?
Inside container env shows:
LANG=en_US.utf8
HOSTNAME=bbc96a3254ed
PG_MAJOR=11
PWD=/
HOME=/root
PG_VERSION=11.2-1.pgdg90+1
GOSU_VERSION=1.11
PGDATA=/var/lib/postgresql/data
POSTGRES_DB=db_test
POSTGIS_MAJOR=2.5
POSTGRES_PASSWORD=p#ssword
POSTGRES_USER=user
SHLVL=1
POSTGIS_VERSION=2.5.2+dfsg-1~exp1.pgdg90+1
...

Unable to connect to the database (development). getaddrinfo ENOTFOUND postgres

I'm trying to connect to a postgres database with a NestJs app. This is my docker-compose file:
postgres:
image: postgres:alpine
ports:
- '5432:5432'
networks:
- network
# volumes:
# - 'postgres_data:/var/lib/postgresql/data'
environment:
POSTGRES_DB: database
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 10s
timeout: 5s
retries: 5
This is my .env file:
TYPEORM_NAME=development
TYPEORM_TYPE=postgres
TYPEORM_HOST=postgres
TYPEORM_PORT=5432
TYPEORM_CACHE=true
TYPEORM_LOGGING=all
TYPEORM_DATABASE=database
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=postgres
TYPEORM_DROP_SCHEMA=false
TYPEORM_SYNCHRONIZE=true
TYPEORM_MIGRATIONS_RUN=true
I run: docker exec printenv and I get these:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=ea6383b9f55f
POSTGRES_PASSWORD=postgres
POSTGRES_DB=database
POSTGRES_USER=postgres
LANG=en_US.utf8
PG_MAJOR=15
PG_VERSION=15.1
When I run npm run start I get this error message:
Unable to connect to the database (development). Retrying (1)...
Error: getaddrinfo ENOTFOUND postgres
at GetAddrInfoReqWrap.onlookup [as oncomplete]
What am I doing wrong?
When you run a program on your host, it can't use the hostnames available on the docker network. It has to use 'localhost' and the mapped port(s) of the container.
So
TYPEORM_HOST=postgres
needs to be
TYPEORM_HOST=localhost
Since you've mapped the container port 5432 to the host port 5432, you don't need to change that.

Cannot access Postgres instance running in Docker container from Pgadmin

I am trying to connect to a Postgres instance running in a Docker container. In the docker-compose file, the postgres service looks like this:
flask-api-postgres:
container_name: flask-api-postgres
image: postgres:13.4-alpine
env_file:
- dev.env
ports:
- "5433:5433"
networks:
flask-network:
With docker inspect I get that the container has the address: 172.19.0.2.
The API works fine, but when trying to access the database from Pgadmin with the config shown in the image (user and password are correctly set), I get the shown error.
Pgadmin config
I do not know how to access the postgres instance from pgadmin.
One approach is you can access the postgres db docker container from pgadmin which is hosted in your host machine using 127.0.0.1 instead of 172.19.0.2
Another way is you can create another container for pgadmin. In this case, you can access your PostgreSQL using container IP (For example: 172.19.0.2). Add this to your docker-compose file
pgadmin:
image: dpage/pgadmin4
depends_on:
- flask-api-postgres
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: admin
restart: unless-stopped
networks:
flask-network:
Make sure both are under same network.
Please check the port you are using. The default is 5432.
See experiment:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c4d92a623a6 postgres:latest "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 5432/tcp, 0.0.0.0:5433->5433/tcp cannot-access-postgres-instance-running-in-docker-container-from-pgadmin-database-1
> docker exec -it 0c4d92a623a6 sh
# psql "host=127.0.0.1 port=5433"
psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
# psql "host=127.0.0.1 port=5432"
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "root" does not exist
#

How are these Docker setups different? One works and the other not

I'm running Postgres image in Docker on an M1 Mac with mapped ports "5432:5432". My app can connect to the DB from the host machine by calling localhost:5432. I'm now trying to run the app within Docker and I'm puzzled by the behavior I see.
This command works:
docker run --name api --add-host host.docker.internal:host-gateway -e DB_HOST=host.docker.internal -p 8000:8000
But when I try to replicate the same by putting the api within the docker-compose like this, it doesn't work:
services:
postgres:
image: postgres:14.2
ports:
- "5432:5432"
networks:
- my-network
api:
image: api
environment:
DB_HOST: host.docker.internal
extra_hosts:
- "host.docker.internal:host-gateway"
Connecting to the DB fails:
failed to connect to host=host.docker.internal user=postgres database=postgres: failed to receive message (unexpected EOF)
I've also tried to put the api container on the same my-network network as postgres, and changing the DB host to be the DB container:
api:
image: api
environment:
DB_HOST: postgres
networks:
- my-network
but that gives me a different error:
failed to connect to host=postgres user=postgres database=postgres: dial error (dial tcp 192.168.192.2:5432: connect: connection refused)
The DB is listening at IPv4 address "0.0.0.0", port 5432 and IPv6 address "::", port 5432. Why would the docker run command work but the other two not work?
As David figured out the issue in the comments, I would like to suggest wait-for-it for such an issue, Instant of waiting a bit then start manually again.
wait-for-it.sh usually located at entrypoint.sh like this
#!/bin/sh
# Wait fot the cassandra db to be ready
./wait-for.sh cassandra:9042 --timeout=0
--timeout=0 , will keep on waiting till cassandra is up and running.
And can be used directly in docker-compose.yaml like the following example :
version: "2"
services:
web:
build: .
ports:
- "80:8000"
depends_on:
- "cassandra"
command: ["./wait-for-it.sh", "cassandra:9042"]
db:
image: cassandra
For more information, You can check Control startup and shutdown order in Compose
Wait-for-it github

Docker-compose: App can not connect to Postgres container

I'm unable to get my Phoenix app connecting to the Postgres container when using docker-compose up.
My docker-compose.yml:
version: '3.5'
services:
web:
image: "solaris_cards:latest"
ports:
- "80:4000"
env_file:
- config/docker.env
depends_on:
- db
db:
image: postgres:10-alpine
volumes:
- "/var/lib/postgresql/data/pgdata/var/lib/postgresql/data"
ports:
- "5432:5432"
env_file:
- config/docker.env
The application running in web container complains that a connection to the Postgres container is non-existing:
[error] Postgrex.Protocol (#PID<0.2134.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db:5432): non-existing domain - :nxdomain
My env variables:
DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASS=postgres
I have tried running the Postgres container first separately and then running the web container but still have the same problem.
If I change the database host to 0.0.0.0 (which is what Postgres shows when running), then it seems to connect but the connection is refused rather than not found.
However docker should be able to translate the host name with out me manually inputing the ip.
Postgres was exiting due to its volume already containing data.
This was solved by cleaning the directory with:
docker-compose down -v