Connecting to Postgres docker container from docker-compose file [duplicate] - postgresql

This question already has answers here:
Communication between multiple docker-compose projects
(20 answers)
Closed 3 years ago.
I have 2 projects. The first contains this docker-compose.yml:
# pg/docker-compose.yml
version: "3"
services:
postgres:
image: postgres
environment:
POSTGRES_USER: pguser
POSTGRES_DB: pg
ports:
- "5432:5432"
volumes:
- "postgres-data:/var/lib/postgresql/data"
networks:
- some_net
volumes:
postgres-data:
networks:
some_net:
driver: bridge
The second project has this docker compose file:
# pg_client/docker-compose.yml
version: "3"
services:
postgres_client:
image: tmaier/postgresql-client
command: postgres://pguser#localhost:5432/pg
networks:
- pg_some_net
networks:
pg_some_net:
external: true
The tmaier/postgresql-client image is extremely simple, it has PostgresSQL installed and runs the command psql DOCKER_COMMAND at build time. In my case this is psql postgres://pguser#localhost:5432/pg.
I can connect to my Postgres db just fine from the command line:
$ psql postgres://pgmuser#localhost:5432/pg
psql (12.1)
Type "help" for help.
pg=#
But when the postgres_client container attempts to connect (after running docker-compose up), it fails saying
postgres_client_1 | psql: could not connect to server: Connection refused
postgres_client_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
postgres_client_1 | TCP/IP connections on port 5432?
postgres_client_1 | could not connect to server: Address not available
postgres_client_1 | Is the server running on host "localhost" (::1) and accepting
postgres_client_1 | TCP/IP connections on port 5432?
Is there something special about docker-compose that I need to account for when writing my Postgres connection URI?
Any help is much appreciated.

The hostname of postgres should resolve to postgres instead of localhost. You can try
command: postgres://pguser#postgres:5432/pg

Related

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 do I connect a pgAdmin Docker container to Postgres running on local machine (non-container)? [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
I'm running pgAdmin in a docker container, trying to connect to Postgres running locally on my machine. No matter what IP address I enter, it gives an error saying :
"Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?"
My server is indeed running, and it is allowing external addresses (listen_addresses = '*').
I guess I'm just not using the correct IP address. What is the correct address I should use?
Here is my docker compose code:
version: '3'
services:
pgadmin:
image: dpage/pgadmin4
ports:
- "5000:80"
environment:
PGADMIN_DEFAULT_EMAIL: me#me.com
PGADMIN_DEFAULT_PASSWORD: admin
network_mode: host
restart: unless-stopped
pgAdmin screenshot here
if you use Linux try set 172.17.0.1 to host
Or host.docker.internal on windows https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds
Mac host.docker.internal https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

Adminer & Migrate Can't Connect to Postgresql Docker Container

I have three docker containers (postgresql, adminer, and go/migrate) and I've exposed both adminer and postgres ports to the host. I can access adminer in my browser, and postico can also connect to the DB. When I try to connect to the db from within adminer, it throws this error:
SQLSTATE[08006] [7] 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? could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
The migrate container throws this error too:
error: dial tcp: 127.0.0.1:5432: connect: connection refused
So, clearly there is an issue with how the containers are able to communicate with each other. Do I need to create a docker network?
version: '3.1'
services:
db:
image: postgres
environment:
POSTGRES_DB: mydbname
POSTGRES_USER: mydbuser
POSTGRES_PASSWORD: mydbpwd
ports:
- "5432:5432"
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mydbpwd#localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
links:
- db
adminer:
image: adminer
restart: always
ports:
- "8081:8080"
depends_on:
- db
You do not need to create linking, docker-compose create default network. also service to service communication should use service name, localhost mean this container(migrate) not DB container.
change localhost to db
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mybpwd#db:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
as you DB service name is db so you connect with db container using it name.

Cannot connect to postgreSQL docker container via postico

I'm trying to use Postico to connect to a docker postgreSQL container on my local machine.
I've tried connecting to 0.0.0.0, localhost, and 127.0.0.1. Each give me the following error:
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?
0.0.0.0 gives me a similar, but smaller error:
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?
Here is my docker-compose file:
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.23
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: prisma
migrations: true
postgres:
image: postgres:10.5
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Solution found thanks to Egor! I forgot to specify ports: - "5432:5432" inside my docker-compose file. Rookie mistake ;)
I also had issues using Postico to connect to my Postgres DB in a docker container.
Ultimately, my issue was that I had a local Postgres DB running.
As soon as I disconnected my local Postgres DB, I was able to use Postico to connect to my docker DB. With the host set to localhost, I used the POSTGRES_USER, POSTGRES_PASSWORD, and host port as defined in my docker-compose.yml file.
If postgres version doesn't matter, try to change Postgres image to this one, it works for me
And also make sure that you add ports in docker-compose.yml
postgres:
image: postgres
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
ports:
- "5432: 5432"
volumes:
- postgres:/var/lib/postgresql/data
P.s. just updated answer for readability

Changing a postgres containers server port in Docker Compose

I am trying to deploy a second database container on a remote server using Docker compose. This postgresql server runs on port 5433 as opposed to 5432 as used by the first postgresql container.
When I set up the application I get this error output:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.17.0.2) and accepting
web_1 | TCP/IP connections on port 5433?
and my docker compose file is:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433"
ports:
- "5433"
volumes:
- ./backups:/home/backups
web:
build: .
command: bash -c "sleep 5 && python -u application/manage.py runserver 0.0.0.0:8081"
volumes:
- .:/code
ports:
- "81:8081"
links:
- db
environment:
- PYTHONUNBUFFERED=0
I feel the issue must be the postgresql.conf file on the server instance having set the port to 5432 causing the error when my app tries to connect to it. Is there a simple way of changing the port using a command in the compose file as opposed to messing around with volumes to replace the file?
I am using the official postgresql container for this job.
Some people may wish to actually change the port Postgres is running on, rather than remapping the exposed port to the host using the port directive.
To do so, use command: -p 5433
In the example used for the question:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433" # Publishes 5433 to other containers but NOT to host machine
ports:
- "5433:5433"
volumes:
- ./backups:/home/backups
command: -p 5433
Note that only the host will respect the port directive. Other containers will not.
Assuming postgres is running on port 5432 in the container and you want to expose it on the host on 5433, this ports strophe:
ports:
- "5433:5432"
will expose the server on port 5433 on the host. You can get rid of your existing expose strophe in this scenario.
If you only want to expose the service to other services declared in the compose file (and NOT localhost), just use the expose strophe and point it to the already internally exposed port 5432.