postgres docker : role "root" does not exist - postgresql

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

Related

Cannot psql into docker postgres image port forwarded locally

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/

psql: error: could not connect to server: FATAL: password authentication failed for user while connecting to dockerized postgres image

I tried to access my Postgres DB that running in docker, here is my docker-compose.yml:
Version: "3.8"
services:
db:
image: postgres
environment:
- POSTGRES_DB=fdc_dashboard
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=saya1234
It successfully run using:
docker-compose up db
but when I tried to connect to it
psql -h localhost -p 5432 -d fdc_dashboard -U admin --password
It raised:
psql: error: could not connect to server: FATAL: password authentication failed for user "admin"
Is there something that I missed?

psql: error: could not connect to server: FATAL: role "postgres" does not exist

I have "dockerized" a Django/PostgreSQL app and try to connect to my database
I have 2 containers: web et db
It works but I can't connect to my postgresql database
I used to ran docker exec -it coverage_africa_db_1 psql -U postgres but I got an error
psql: error: could not connect to server: FATAL: role "postgres" does not exist
I try to 'jump' into my container by running the command docker exec -it aab213f730cd bash and try to connect using psql command...
psql -d db_dev
psql: error: could not connect to server: FATAL: role "root" does not exist
or
psql -U postgres
error: could not connect to server: FATAL: role "postgres" does not exist
in fact, none of psql options works...
.env.dev
SECRET_KEY=*************************************
DEBUG=1
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=db_dev
SQL_USER=user
SQL_PASSWORD=user
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
DJANGO_SETTINGS_MODULE=core.settings.dev
docker-compose.yml
version: '3.7'
services:
web:
build: ./app
restart: always
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
restart: always
volumes:
- postgres_data:/var/lib/postgres/data/
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
volumes:
postgres_data:
With postgres container, this:
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=user
- POSTGRES_DB=db_dev
defines how the database is initialized. If you didn't change it, you should be able to connect as user 'user' with password 'user'.
If you did change it, then the actual values are those which were present at the first launch. After first launch those credentials are written into the database, which data is on postgres_data volume. If you want to delete the data and reinitialize database with new credentials, use docker-compose down -v.

How to connect to postgres created with docker-compose from outside host

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.

postgres with docker compose gives FATAL: role "root" does not exist error

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