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"
}
}
}
Related
I'm trying to connect to my local server. I have the following docker-compose.yaml and servers.json files. I think I'm making a mistake with my .pgpass. When the containers are up and running I can login to pgAdmin, and I can see the docker_postgres_group "group", but when I try to login I get the authentication error in the first screenshot below. Further below is how I've got my pgpass set up. I think this is where the problem is, but I could be wrong...
docker-compose.yaml
version: '3.8'
services:
db:
container_name: pg_container
image: postgres
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
volumes:
- ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
- ./pgpass:/pgpass # passwords for the connections in this file
servers.json
{
"Servers": {
"1": {
"Name": "docker_postgres",
"Group": "docker_postgres_group",
"Host": "host.docker.internal",
"Port": 15432,
"MaintenanceDB": "postgres",
"Username": "postgres",
"PassFile": "/pgpass",
"SSLMode": "prefer"
}
}
}
.pgpass
This is what's inside my pgpass file: host.docker.internal:15432:postgres:postgres:postgres
Here's its location:
I've tried having the .pgpass file in the pgpass folder and outside.
And - I'm using postgres as the password to try to login to pgadmin.
When spinning up containers through docker-compose, also a network is created where each service can see the other services of the docker-compose definition. They are also resolvable by there servicename. In your case you can replace host.docker.internal with db and use the internal port 5432.
EDIT: host.docker.internal resolves the internal IP address used by your host machine. If you want connect through the host IP, the you have either to change the port in servers.json from 15432 to 5432 or the port mapping from the db service:
ports:
- "15432:5432"
On my rasperry pi 4 I've installed docker and docker-compose and now I'm tring to install and use Postgres and Adminer
following that https://hub.docker.com/_/postgres I've created docker-compose.yaml file as follow:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
and i run it with
docker-compose -f docker-compose.yaml up -d
after that DB_1 starts and adminer too
but when i try connect to http://192.168.1.38:8080/ i can't reach it
even if i try connect to postgres through pgAdmin it's says
could not connect to server: Connection refused (0x0000274D/10061) Is
the server running on host "192.168.1.38" and accepting TCP/IP
connections on port 5432?
however if i don't use docker-compose but just
docker run --name postgres -d --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=123456 -v ${PWD}/data:/var/lib/postgresql/data postgres
it's work through pgAdmin
do you know what i'm doing wrong?
UPDATE: seems the problem is with docker-compose because any kind of docker-compose.yml file block connection to it...
with a container with djgango i tried to start server and it's works but when i try reach page it seem bloccked too
when i run docker-compose.yaml file docker-compose ps output is:
sudo netstat -tulpn screenshot
PgAdmin can't reach 5432 ports because you don't expose it.
Like for Adminer you need to expose the Postgres port 5432 on your machine in your compose file.
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
a little late to the party but what you need to do is figure out the IP address of the postgres container, and use that as your host.
here is my docker-compose.yml file:
version: "3.5"
services:
db:
image: myapp
container_name: my-database
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypasswd
POSTGRES_DB: mydb
expose:
- "5432"
ports:
- 8000:5432
volumes:
- /path/to/my/migrationsV1_0__audit_table.sql
- //path/to/my/migrations/V1_1__tables.sql
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
Then i $ docker inspect xxxxxxxx | grep "IPAddress"
It produces output:
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
After docker-compose I enter values to pgadmin as so:
Pgadmin_capture
Yet pgadmin gives error:
Unable to connect to server
timeout expired.
What is wrong here?
In your configuration PostgreSQL database has 5432 port (it is default value).
Are you sure in this line?
PGADMIN_LISTEN_PORT=5454
Try to replace it with
PGADMIN_LISTEN_PORT=5432
for a workaround to this issue, just run the container with --user=root (in docker-compose use user: root)
You need to use the gateway address of Postgres container. Use docker inspect xxxxxxxx | grep Gateway to get it. I guess in your case it would be 172.17.0.1
In my case, my pgadmin container didn't shared the DB container's network.
So, you should run this command.
docker network inspect NETWORK <YOUR DB CONTAINER'S NETWORK>
And if it doesn't, run this command
docker network connect <YOUR_DB_CONTAINER'S_NETWORK> <YOUR_PGADMIN_CONTAINER'S NAME>
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
Given this docker-compose.yml, am experiencing difficulties connecting the docker stack to my pgAdmin.
version: '3.1'
services:
database:
image: postgres
restart: always
environment:
POSTGRES_USER: db-user
POSTGRES_PASSWORD: db-user
POSTGRES_DB: db
ports:
- 5432:5432
For the pgAdmin Connection properties, here's what I've used (others, default values):
Host: 127.0.0.1
Username & Password: db-user
And for the error message when saving:
Error saving properties: UNAUTHORIZED
Unable to connect to server
The docker postgresql page suggest the stack.yml:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
And then visit http://localhost:8080.
Or:
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" ...
In your case, adding port to the database itself suggests your queries should use that port.