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
Related
I have a docker-compose file:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: ayyy
POSTGRES_USER: letsgo
POSTGRES_PASSWORD: pwpwpwpw22
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: user#example.com
PGADMIN_DEFAULT_PASSWORD: pwpwpwpw1
ports:
- "5433:80"
(I changed the environment variables to not spoof my credentials)
However I am unable to connect to my Postgres server through pgAdmin. pgAdmin is using reverse proxy from port 5433 to my subdomain pgadmin.domain.com. I am also reverse proxying HTTP from pgAdmin's container into HTTPS. (I do not know if that could be an issue)
This is the error I get:
Unable to connect to server:
connection to server at "db" (192.168.32.3), port 5432 failed: timeout expired
If I use localhost as a hostname, I get this:
Unable to connect to server:
connection to server at "localhost" (127.0.0.1), port 5432 failed:
Connection refused Is the server running on that host and accepting
TCP/IP connections? connection to server at "localhost" (::1), port
5432 failed: Address not available Is the server running on that host
and accepting TCP/IP connections?
UPDATE: I am not even able to ping the containers between each other. The service name is correctly resolved to IP, but I get no response.
I was even unable to open ports on my server, so I reinstalled it. Everything works as it should since then.
I am new to Docker containers but not quite new to Postgres (I delved a bit in the past).
The thing is I am trying to connect to a Postgres server started from the following Docker container dpage/pgadmin4
The issue is that I cannot connect to the server using PGAdmin4.
I am getting the following error:
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?
I have the following yaml file:
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: parola
PGDATA: /var/lib/postgresql/data/pgdata
pgadmin4:
image: dpage/pgadmin4
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: user#domain.com
PGADMIN_DEFAULT_PASSWORD: parola
Using the psql command line I can connect to it only if I set -h 0.0.0.0, as follows:
psql -h 0.0.0.0 -U postgres
Can someone tell me what I need to set up in the yaml file or how I can configure so it allow connection from localhost/127.0.0.1?
I am running on Ubuntu 20.04.2, AMD64 with Docker version 20.10.7, build f0df350.
service pgadmin4 is not being able reach service db because inside pgadmin4 at port 5432 there wont be any service running
With this configuration, you are only mapping the db to host machine's 5432 port. That is why you were able to run psql -h 0.0.0.0 -U postgres in your machine (host machine)
According to docker-compose pgadmin4 can access DB using host name db
you can link the configuration with some additional configuration as below
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: parola
PGDATA: /var/lib/postgresql/data/pgdata
pgadmin4:
image: dpage/pgadmin4
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: user#domain.com
PGADMIN_DEFAULT_PASSWORD: parola
volumes:
- ./servers.json:/pgadmin4/servers.json
add a file called servers.json in the same directory as docker-compose with the below contents. take note that I have added the Host as db
{
"Servers": {
"1": {
"Name": "local db",
"Group": "Server Group 1",
"Port": 5432,
"Username": "postgres",
"Host": "db",
"SSLMode": "prefer",
"MaintenanceDB": "postgres"
}
}
}
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.
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
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.