docker compose psql: error: FATAL: role "postgres" does not exist - postgresql

I faced a problem when I try to use psql command with my docker-compose file on my local Ubuntu machine:
psql: error: FATAL: role "postgres" does not exist
I tried to use others solution like removing docker image, volume. psql -U postgres doesn't work for me either.
I try to use first docker-compose up, then docker exec -it database bash
There's my docker-compose file
services:
db:
container_name: postgres
image: postgres:13.3-alpine
restart: always
user: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=root
ports:
- "5432:5432"
volumes:
- ./data/db:/var/lib/postgresql/data
Maybe this string tells something?
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
OUTPUT:
Attaching to postgres
postgres |
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres |
postgres | 2021-08-02 17:29:10.426 UTC [1] LOG: starting PostgreSQL 13.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424, 64-bit
postgres | 2021-08-02 17:29:10.426 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2021-08-02 17:29:10.426 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres | 2021-08-02 17:29:10.429 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2021-08-02 17:29:10.433 UTC [12] LOG: database system was shut down at 2021-08-02 17:22:17 UTC
postgres | 2021-08-02 17:29:10.438 UTC [1] LOG: database system is ready to accept connections
postgres | 2021-08-02 17:37:53.452 UTC [33] FATAL: role "postgres" does not exist
postgres | 2021-08-02 17:37:56.958 UTC [35] FATAL: role "user" does not exist
postgres | 2021-08-02 17:41:54.294 UTC [45] FATAL: role "postgres" does not exist```

First, you've set POSTGRES_USER to root, so you're going to have a root user instead of postgres user.
Second, if a database already exists, it doesn't matter what you set for POSTGRES_USER and POSTGRES_PASSWORD -- postgres will use whatever is in the database.
So you can either:
Delete the database (rm -rf data/db) and start over, or
Edit your pg_hba.conf so that you don't need a password

docker-compose exec web rake db:migrate db:create - I have above error: role "..." does not exist.
When I try:
docker-compose exec web rake db:drop
docker-compose exec web rake db:setup
it's working.

Related

Can't connect to Docker Hosted Postgres

My attempt to start a PostgreSQL Docker container fails when doing docker-compose. I can connect to it if I do docker run instead. I don't know what the difference is. My intent with the compose is to have it create a database usable for development and that I can connect to with user postgres.
docker-compose.yml:
version: '3.7'
services:
my_app:
build:
dockerfile: ./dockerfile-my-app
context: .
command: tail -f /dev/null
depends_on:
- db
env_file: ./.env.development
ports:
- "8080:8080"
volumes:
- /c/Users/woodsman/linux-mint-woodsman/home/dev:/home/dev
db:
image: postgres:latest
env_file: ./.env.development
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=helloworld
- PGDATA=/var/lib/postgresql/data/helloworld2/
ports:
- "5432:5432"
restart: "no"
volumes:
- /c/Users/woodsman/linux-mint-woodsman/dbdata:/var/lib/postgresql/data/pgdata
volumes:
dbdata:
Attempting to connect to jdbc:postgresql://localhost:5432/helloworld using user postgres and password postgres. Please pardon any security concerns you may perceive. This will be tightened for development, and done much more securely when deployed to production.
The reported error as reported by SquirrelSQL is:
hello-world: FATAL: password authentication failed for user "postgres"
class org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
The image hashcode is:
9dbc24674f25eb449df11179ed3717c47348fb3aa985ae14b3936d54c2c09dde
>docker logs c4c2
2022-05-16 05:28:50.560 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-16 05:28:50.560 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-05-16 05:28:50.560 UTC [1] LOG: listening on IPv6 address "::", port 5432
2022-05-16 05:28:50.564 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-05-16 05:28:50.571 UTC [63] LOG: database system was shut down at 2022-05-16 05:28:50 UTC
2022-05-16 05:28:50.579 UTC [1] LOG: database system is ready to accept connections
2022-05-16 05:29:17.184 UTC [70] FATAL: password authentication failed for user "postgres"
2022-05-16 05:29:17.184 UTC [70] DETAIL: Role "postgres" does not exist.
Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
2022-05-16 05:36:41.983 UTC [78] FATAL: password authentication failed for user "postgres"
2022-05-16 05:36:41.983 UTC [78] DETAIL: Role "postgres" does not exist.
Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
I hope for a Docker based answer. If there's some PostgreSQL command I need to do (or not do), please tell me how I can pass that through Docker.
Thanks,
Woodsman
Your main error is:
Role "postgres" does not exist.
It seems because whitespace here in docker-compose.yml:
POSTGRES_USER= postgres
In first start postgres was initialized, so when You change "POSTGRES_USER" in variable, role will not create. You can delete you volume and start docker-compose again.

How to use Netcat to check whether postgresql docker container is up

I want to see if docker container with PostgreSQL is ready using Netcat utility.
My entrypoint.sh script seems to be unable to spot the DB up and running.
When I login into docker and run NC in verbose mode I get
DNS fwd/rev mismatch: db != telegram_messages_db_1.telegram_messages_default
What am I doing wrong?
My setup
entrypoint.sh
echo "Waiting for postgres..."
while ! nc -z db 5432; do
sleep 0.1
done
echo "PostgreSQL started"
docker-compose.yaml
version: '3.7'
services:
messages:
build:
context: .
dockerfile: Dockerfile
entrypoint: ['/usr/src/app/entrypoint.sh'] # new
volumes:
- .:/usr/src/app
ports:
- 5001:5000
environment:
.....
depends_on:
- db
db:
build:
context: ./backend/db
dockerfile: Dockerfile
expose:
- 5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
docker-compose output
Attaching to telegram_messages_db_1, telegram_messages_messages_1
messages_1 | Waiting for postgres...
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2020-03-29 23:55:22.103 UTC [1] LOG: starting PostgreSQL 12.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
db_1 | 2020-03-29 23:55:22.103 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-03-29 23:55:22.104 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-03-29 23:55:22.115 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-03-29 23:55:22.170 UTC [20] LOG: database system was shut down at 2020-03-29 23:46:56 UTC
db_1 | 2020-03-29 23:55:22.190 UTC [1] LOG: database system is ready to accept connections
Also...
If I run nc -z -v telegram_messages_db_1.telegram_messages_default 5432
I do get a nice response:
telegram_messages_db_1.telegram_messages_default [172.19.0.2] 5432 (postgresql) open
Removing all containers, rebooting and building from scratch solved the issue.
I will leave it here for posterity.

Docker-Compose + postgres: database and user creation not working (works without compose)

The docs for the postgres Docker image explain that you can make the image create a user and database on creation, using environment variables.
I can't seem to make that work using docker-compose:
# docker-compose.yml
services:
postgresql:
image: postgres:alpine
environment:
POSTGRES_DB: iotplatform
POSTGRES_USER: iotplatform
POSTGRES_PASSWORD: iotplatform
and here's what I run:
docker-compose up -d --force-recreate postgresql
docker-compose exec postgresql psql -U iotplatform
# psql: FATAL: role "iotplatform" does not exist
When I run docker-compose exec postgresql env, I see the environment variables as configured.
The logs don't say anything particular:
Attaching to iot-container-tracker_postgresql_1
postgresql_1 | 2018-12-17 17:35:05.754 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgresql_1 | 2018-12-17 17:35:05.754 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgresql_1 | 2018-12-17 17:35:05.757 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_1 | 2018-12-17 17:35:05.770 UTC [21] LOG: database system was shut down at 2018-12-17 17:35:03 UTC
postgresql_1 | 2018-12-17 17:35:05.772 UTC [1] LOG: database system is ready to accept connections
postgresql_1 | 2018-12-17 17:35:22.639 UTC [34] FATAL: role "iotplatform" does not exist
EDIT: tried without docker-compose and it works.
docker run --name some-postgres -e POSTGRES_PASSWORD=iotplatform -e POSTGRES_DB=iotplatform -e POSTGRES_USER=iotplatform -d postgres:alpine
docker exec -it some-postgres psql -U iotplatform
iotplatform=#
What am I missing?
After running down and up it works. Apparently --force-recreate wasn't a hard enough reset.
Thanks #StéphaneJeandeaux for your help.

How to setup volume for postgres log folder - Permission denied error

Currently, I would like to mount logging folder for postgres, so that even after host machine restart, logging information is still preserved.
I use the following docker-compose.yml
docker-compose.yml
version: '2'
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=55F4rGFwsXXXXXX
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- postgres_logs:/logs
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs
volumes:
postgres_data:
postgres_logs:
However, I'm getting the following permission denied error.
postgres_1 | 2018-02-19 09:03:45.359 UTC [1] LOG: database system is shut down
postgres_1 | 2018-02-19 09:04:45.963 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2018-02-19 09:04:45.963 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2018-02-19 09:04:45.965 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2018-02-19 09:04:45.972 UTC [1] FATAL: could not open log file "/logs/postgresql-2018-02-19_090445.log": Permission denied
postgres_1 | 2018-02-19 09:04:45.974 UTC [1] LOG: database system is shut down
postgres_1 | 2018-02-19 09:05:46.741 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2018-02-19 09:05:46.741 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2018-02-19 09:05:46.744 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2018-02-19 09:05:46.753 UTC [1] FATAL: could not open log file "/logs/postgresql-2018-02-19_090546.log": Permission denied
postgres_1 | 2018-02-19 09:05:46.755 UTC [1] LOG: database system is shut down
postgres_1 | 2018-02-19 09:06:47.366 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2018-02-19 09:06:47.366 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2018-02-19 09:06:47.368 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2018-02-19 09:06:47.375 UTC [1] FATAL: could not open log file "/logs/postgresql-2018-02-19_090647.log": Permission denied
postgres_1 | 2018-02-19 09:06:47.377 UTC [1] LOG: database system is shut down
Anyone has idea how can I resolve such error?
2nd Edit
After some reading it seems that is not possible to do this the way you need. You would need to be able to define file ownership when declaring a volume with docker-compose, and this is something that is not supported by the docker engine. But there are a few workarounds that you can consider, check here for more details.
As a workaround you could create a Dockerfile extending postgres and add this:
# ...
RUN mkdir /logs
RUN chown postgres:postgres /logs
Edit
After some experimentation this is working:
version: '2'
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=55F4rGFwsXXXXXX
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./:/logs:z
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs
volumes:
postgres_data:
Original answer
I accidentally got it working by doing this, first running docker-compose up with this docker-compose file:
version: '2'
services:
postgres:
image: postgres:latest
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=55F4rGFwsXXXXXX
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- postgres_logs:/logs:z
# Make Postgres log to a file.
# More on logging with Postgres: https://www.postgresql.org/docs/current/static/runtime-config-logging.html
command: /bin/bash -c "mkdir -p /logs && chmod -R 777 /logs && postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/logs"
volumes:
postgres_data:
postgres_logs:
This fails with:
postgres_1 | "root" execution of the PostgreSQL server is not
permitted. postgres_1 | The server must be started under an
unprivileged user ID to prevent postgres_1 | possible system security
compromise. See the documentation for postgres_1 | more information
on how to properly start the server.
After reverting the changes to command and running again then works, but obviously this is not a solution so stick with the edited answer above.
the problem is the permission in host, not in container.
so in host,
chmod -R 777 postgres_logs

Docker compose and postgres official image environment variables

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