问题
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?
回答1:
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
with the -U Postgres
flag, or su - Postgres
first
See also the postgresql documentation
回答2:
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
来源:https://stackoverflow.com/questions/60193781/postgres-with-docker-compose-gives-fatal-role-root-does-not-exist-error