I think this problem should be very easy to solve, but I am pulling my hair out. I have the following docker-compose:
version: "3"
services:
pg-db:
image: postgres:11
environment:
POSTGRES_PASSWORD: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_DB: "postgres"
ports:
- 5432:5432
#network_mode: host
Then I run docker-compose up and it starts the container
pg-db_1_78e7ec4a95e6 | 2020-02-21 13:53:53.928 UTC [1] LOG: database system is ready to accept connections
I can connect to it with docker exec
docker exec -it docker_pg-db-compose_1_78e7ec4a95e6 psql -h pg-db -U postgres postgres
But I can't manage to connect to it by 'naked' psql:
psql postgresql://postgres:postgres#localhost:5432/postgres
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
psql postgresql://postgres:postgres#pg-dg:5432/postgres
psql: could not translate host name "pg-db" to address: Name or service not known
I've tried with network_mode but it doesn't help.
After starting docker-compose, you need to get the docker container ip by doing this:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
Then you can do postgresql://postgres:postgres#<container idp>:5432/postgres you should be able to connect to it
I'm using a modified version of the docker-compose file provided in the Postgres DockerHub page.
version: '3.1'
services:
db:
image: postgres:11
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: postgres
When I run docker-compose up I get a very similar "ready" message:
db_1 | 2020-02-21 14:34:17.826 UTC [1] LOG: database system is ready to accept connections
At this point, I can connect to Postgres using psql.
$ psql -h localhost -U postgres
Password for user postgres:
psql (12.2, server 11.7 (Debian 11.7-1.pgdg90+1))
Type "help" for help.
postgres=#
I'm also able to connect using the connection string that you provided, and postico:
The differences are subtle, I'm not sure if the restart policy makes any difference here, but give it a try.
Related
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
#
I'm continuously hitting an error when trying to psql into a docker composed postgres image that has its ports forwarded. (this issue seems to persist also when attempting to access the DB programatically via node application).
Running docker-compose up -d on the following docker compose file:
services:
postgres:
container_name: cnc-matches
image: postgres:12.1-alpine
ports:
- '5432:5432'
environment:
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: pass
POSTGRES_DB: cnc-matches
When running psql to attempt to access it I hit the following error continuously:
C:\Users\danie\Desktop\dev\cnc-db\db-setup>psql -h "localhost" -p "5432" -U dbuser
Password for user dbuser: pass
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "dbuser"
When running docker exec I'm able to access the table and info fine:
C:\Users\danie\Desktop\dev\cnc-db\db-setup>docker exec -it cnc-matches psql -U dbuser cnc-matches
psql (12.1)
Type "help" for help.
cnc-matches=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
dbuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
I've tried creating a new user as well as altering the dbuser profiles passwords in here with ALTER PASSWORD dbuser WITH PASSWORD 'pass' and I still cannot access the db with default psql command locally.
cnc-matches=# CREATE USER tester WITH PASSWORD 'tester';
CREATE ROLE
cnc-matches=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
dbuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
tester | | {}
C:\Users\danie\Desktop\dev\cnc-db\db-setup>psql -h "localhost" -p "5432" -U tester
Password for user tester: tester
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "tester"
Not sure what it is I'm misisng here, if relevant running via windows 11 cmd.
Any help/suggestions appreciated.
Looks like you also need to include the database name when connecting to the database running in the Docker container, ex
psql -h "0.0.0.0" -p "5432" -U dbuser cnc-matches
If the container was successfully started, try running:
psql -U dbuser -d cnc-matches -h 0.0.0.0 --port 5432
For the port use whatever you mapped yours in the container to; in your case 5432.
You must specify all that explicitly or else you will get an error that the database cannot accept TCP/IP connections.
First time I am posting, sorry if there is wrong formatting.
I updated your docker-compose file with a network bridge as still isolated from the network with the host machine.
Steps:
"db-net" can be any name you want but both must coincide with your docker compose file
Create a docker network with: docker network create db-net
Run: docker-compose up -d
version: "3.9"
services:
postgresCNC:
container_name: cnc-matches
image: postgres:15.0-alpine
ports:
- '5432:5432'
environment:
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: pass
POSTGRES_DB: cnc-matches
networks:
db-net:
networks:
db-net:
driver: bridge
Network bridge by default uses the hardware network device to communicate between containers and host machine as with containers with others containers.
More info here:
https://docs.docker.com/network/bridge/
Windows 10-Pro. Have a local Postgres installed and working fine.
With it running, VSC terminal, docker-compose up the following ok:
version: '3.8'
services:
postgres:
image: postgres:10.4.2
ports:
- '5432:5432'
volumes:
- ./sql:/docker-entrypoint-initdb.d
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass1
POSTGRES_DB: db
But PSQL shell always complain password authentication failed for user.
Stopping postgres service from Windows Services and docker-compose up, PSQL shell authentication and query ok. But VSC terminal keep complaining another thing:
FATAL: password authentication failed for user "postgres"
DETAIL: User "postgres" has no password assigned.
Connection matched pg_hba.conf line 95: "host all all all md5"
How to stop the above error when docker container's instance is running? Also, possible to co-run both local and docker?
Hope you are enjoying you containers journey !
I tried to execute your docker-compose as it was but cannot fetch the postgres:10.4.2 image:
❯ docker-compose up
[+] Running 0/1
⠿ postgres Error 2.1s
Error response from daemon: manifest for postgres:10.4.2 not found: manifest unknown: manifest unknown
so i decided to use postgres:14.2 instead. Since I dont have your sql script i'll comment out the volume section.
Here is how my docker-compose looks like:
version: '3.8'
services:
postgres:
image: postgres:14.2
ports:
- '5432:5432'
# volumes:
# - ./sql:/docker-entrypoint-initdb.d
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass1
POSTGRES_DB: db
So, when a execute the compose I got this:
❯ docker-compose up -d
[+] Running 1/1
⠿ Container postgre-local-and-dockercompose-71984505-postgres-1 Started
❯ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
4b90573f6108 postgres:14.2 "docker-entrypoint.s…" 18 seconds ago Up 15 seconds 0.0.0.0:5432->5432/tcp postgre-local-and-dockercompose-71984505-postgres-1
When i connect to the container with:
❯ docker exec -it postgre-local-and-dockercompose-71984505-postgres-1 bash
root#4b90573f6108:/#
and execute this command to connect to your created DB and connect with the "pass1" password:
root#4b90573f6108:/# psql --username=$POSTGRES_USER -W --host=localhost --port=5432 --dbname=$POSTGRES_DB
Password:
psql (14.2 (Debian 14.2-1.pgdg110+1))
Type "help" for help.
db=#
everything is fine.
So I advise you to use the same postgres:14.2 image i tried with (patched with the last security issues) and do the same test.
If you want me to test exactly what you are doing just send your sql scripts.
To answer your second question, yes it is possible to co-run both local and docker postgres instances
you just have to port-forward the postgresql port of your container to another port like this:
version: '3.8'
services:
postgres:
image: postgres:14.2
ports:
- '5433:5432'
# volumes:
# - ./sql:/docker-entrypoint-initdb.d
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass1
POSTGRES_DB: db
since there is no port conflict (your local db is running on 5432 and your docker db on 5433), everything will work fine (I will use dbeaver to try to connect ):
PERFECT !
Hope I answered your questions.
bguess.
I'm trying to create a simple demo with postgres on a local windows machine with docker desktop.
This is my yaml docker compose file named img.yaml:
version: '3.6'
services:
postgres-demo:
image: postgres:11.5-alpine
container_name: postgres-demo
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=Welcome
- POSTGRES_DB=conference_app
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
ports:
- 5432:5432
volumes:
- .:/var/lib/my_data
restart: always
I'm running it using the command:
docker-compose -f img.yaml up
And get the following output:
Starting postgres-demo ... done
Attaching to postgres-demo
postgres-demo | 2020-02-12 17:07:46.487 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres-demo | 2020-02-12 17:07:46.487 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres-demo | 2020-02-12 17:07:46.508 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-demo | 2020-02-12 17:07:46.543 UTC [18] LOG: database system was shut down at 2020-02-12 17:07:10 UTC
postgres-demo | 2020-02-12 17:07:46.556 UTC [1] LOG: database system is ready to accept connections
And then, opening bash into the container with the command:
docker exec -it d47056217a97 bash
I want to watch the databases in container so I run in the bash the command:
psql \dt
And get the error:
psql: FATAL: role "root" does not exist.
Trying to create the database using the command: psql> create database conference_app; gives the error: psql: FATAL: role "conference_app" does not exist.
I'm puzzled. What am I doing wrong? Is my yaml missing something?
If you don’t specify the PGUSER environment variable, then psql will assume you want to use the current OS user as your database user name. In this case, you are using root as your OS user, and you will attempt to log in as root, but that user doesn’t exist in the database.
You’ll need to either call psql -U postgres, or su - Postgres first
See also the postgresql documentation
UPDATE: Someone suggested changing PGUSER to POSTGRES_USER -- this is actually incorrect. Postgres looks for PGUSER in the environment, but if you're using Docker, you'll tell Docker the correct user by using POSTGRES_USER, which gets assigned to PGUSER -- see the entrypoint source code
I specified user: postgres for the service in docker-compose file. Then deleted the existing container to re-spin it with the next docker-compose up execution. Container came up with the user "postgres", so you just need psql -l from there onwards(don't need -U flag)
This was my docker-compose file
version: '3.1'
services:
db:
image: postgres:12.6-alpine
restart: always
container_name: postgres12_6
user: postgres
environment:
- "POSTGRES_PASSWORD=postgres"
- "ES_JAVA_OPTS=-Xms1024m -Xmx3072m"
networks:
- esnet
adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
esnet:
The container assumes that you are trying to connect to db with root user(current user) since you dont specify the user and database name on docker exec.
Following should work:
docker exec -it <container_id/container_name> psql -U <user_name> <database_name>
docker exec -it d47056217a97 psql -U postgres conference_app
in my case, I was using windows 10, I tried everything I could but still not works.
finally, I removed all the related docker image, docker container, local host folder, and restart windows.
and everything goes well
Actually I'm using the following docker-compose.yml file
version: '3.3'
services:
postgres:
container_name: postgres
image: postgres:latest
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
volumes:
- ./data/postgres/pgdata:/var/lib/postgresql/data/pgdata
I use also this .env file in the same directory of the docker-compose.yml file:
POSTGRES_USER=dbadm
POSTGRES_PASSWORD=dbpwd
POSTGRES_DB=db
Then I run a bash shell into container this way:
docker exec -ti postgres bash
And after this invoke the command:
psql -h postgres -U dbadm db
And I get the error:
psql: FATAL: password authentication failed for user "dbadm"
The strange fact is that if use the default image parameters:
psql -h postgres -U admin database
And insert the default password "password", it logs me in, and seems it's ignoring the environment variables.
What am I missing?
Additional logs from docker-compose up logs:
postgres | 2017-10-15 09:19:15.502 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2017-10-15 09:19:15.502 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres | 2017-10-15 09:19:15.505 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2017-10-15 09:19:15.524 UTC [22] LOG: database system was shut down at 2017-10-15 09:02:21 UTC
postgres | 2017-10-15 09:19:15.530 UTC [1] LOG: database system is ready to accept connections
Cannot see any "RUN" line about user, database and password setup.
according to https://hub.docker.com/_/postgres documentation.
Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.
I have created a docker-compose yml and .env file with the details you've provided and everything works fine as you can see from the pictures below:
I think your problem lies when you are passing the -h parameter.
Inside the container will always be localhost however, outside you will have to pass:
hostname: postgres
inside your docker-compose file so it will have the postgres hostname