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/
Related
I'm trying to connect weblate to external rds postgres database.
I'm using docker compose file that run weblate container. To this container I add the environment variables to connect to rds postgres.
The weblate container doesn't connect to rds postgres and give me this error:
psql: error: connection to server at "XXXX.rds.amazonaws.com", port 5432 failed: FATAL: password authentication failed for user "postgres"
but if I try to connect to rds postgres from inside the weblate container via cli, it works.
docker compose file:
version: '3'
services:
weblate:
image: weblate/weblate
tmpfs:
- /app/cache
volumes:
- weblate-data:/app/data
env_file:
- ./environment
restart: always
ports:
- 80:8080
environment:
POSTGRES_PASSWORD: XXXX
POSTGRES_USER: myuser
POSTGRES_DATABASE: mydb
POSTGRES_HOST: XXX.rds.amazonaws.com
POSTGRES_PORT: 5432
It tries to connect as postgres user while your configuration states myuser. Maybe the ./environment file overrides that?
I found the problem.
The problem was the char $ inside password.
Maybe the library used to connect to postgres has a bug or, simple, not allowed $ in password string.
When I removed that char it works.
I am running docker-compose in Github-Action. docker-compose.yml has following service definition for postgres
postgres:
container_name: postgres
image: postgres:12
restart: always
volumes:
- ./test/data/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
POSTGRES_DB: "pgdb"
POSTGRES_USER: "pguser"
POSTGRES_PASSWORD: "fr2Yitl4BgX"
ports:
- "${POSTGRES_PORT:-5432}:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
networks:
- "local-api"
But when container start on serf-hosted Github-Action runner, I see following
postgres | 2021-12-02 19:48:33.537 UTC [414] FATAL: role "root" does not exist
postgres | 2021-12-02 19:48:43.984 UTC [424] FATAL: role "root" does not exist
postgres | 2021-12-02 19:48:54.265 UTC [433] FATAL: role "root" does not exist
postgres | 2021-12-02 19:49:04.410 UTC [443] FATAL: role "root" does not exist
What is missing here ?
should set POSTGRES_DB and POSTGRES_USER in healthcheck
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
I'm getting a similar error when trying to run a pg_restore on the postgresql docker container, I had set up postgresql on docker-compose and was trying to restore a db (from my prev postgresql) to it. Just had the initial user as "postgres". Some errors I'd get:
pg_restore: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "root" does not exist
createuser: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist
On research I found my pg_restore command needed to specify the User for it to work (tried specifying role, that failed)
Here's the working pg_restore command. Perhaps you can do similarly and find a way to specify user in yours?
#on docker container of psql, command that failed
pg_restore --verbose --clean --no-acl --no-owner -h localhost --role=postgres -d thedbname /home/psql_backups/psql_myapp_220423.dumpbackup
#command that worked
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d thedbname /home/psql_backups/psql_myapp_220423.dumpbackup
# lastly (for those doing a pg_restore), from psql commandline, if the restore seems to have worked but don't see data, you may be on the wrong db
\c
\c theDbToSwitchTo
Context
I need to populate a database inside a docker container from a backup file that I have on the host machine.
I've tried this docker command while the PostGIS container is up (see docker-compose.yml at the end):
sudo docker exec -i db_container_1 ./usr/local/bin/pg_restore --no-owner --role=postgres -h localhost -U postgres -p 5434 -d database_name < ../db/dumps/dump_prod_2020.backup
But I've got this message:
read unix #->/var/run/docker.sock: read: connection reset by peer
As documented here, I also tried using this docker-compose command but it raises the exact same strange message:
docker-compose exec -T db /usr/local/bin/pg_restore --no-owner --role=postgres -h localhost -U postgres -p 5434 -d database_name < ../db/dumps/dump_prod_2020.backup
Question
What am I doing wrong and how could I populate my docker database with my local dump?
More infos
Here's the docker-compose.yml used to start the db service (docker ps outputs db_container_1 as the corresponding container name):
version: '3.6'
volumes:
db_data:
services:
db:
image: mdillon/postgis:11-alpine
environment:
POSTGRES_DB: database_name
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- '${DB_PORT:-5434}:5432'
restart: 'no'
volumes:
- './docker/db:/docker-entrypoint-initdb.d:ro'
- 'db_data:/var/lib/postgresql/data' # to persist storage
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.
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