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

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

Related

Postgres, Local installed instance interfered with Docker instance

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.

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.

Docker compose cannot run command service not running

I created docker-compose.yml which content you can find below. I navigate to the folder where file resist and run command:
docker-compose up -d
This was shown:
Starting postgres ... done
then i run that query:
docker-compose ps
Result:
Name Command State Ports
---------------------------------------------------------
postgres docker-entrypoint.sh postgres Exit 1
Now i wanted to run some command:
docker exec -it postgres psql -h localhost -p 54320 -U robert
This is what i get:
Error response from daemon: Container ae1565a84bcf0b3662b47d4f277efd2830273554b6bcf4437129e33b31c88b35 is not running
Is my container not running or? please of support.
docker-compose.yml:
version: "3"
services:
# Create a service named db.
db:
# Use the Docker Image postgres. This will pull the newest release.
image: "postgres"
# Give the container the name my_postgres. You can changes to something else.
container_name: "postgres"
# Setup the username, password, and database name. You can changes these values.
environment:
- POSTGRES_USER=robert
- POSTGRES_PASSWORD=robert
- POSTGRES_DB=mydb
# Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
ports:
- "54320:5432"
# Set a volume some that database is not lost after shutting down the container.
# I used the name postgres-data but you can changed it to something else.
volumes:
- ./volumes/postgres:/var/lib/postgresql/data
Can you attempt exec
docker run -it postgres psql -h localhost -p 54320 -U robert
?
$ docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Since your container has the status exit, you can't use docker exec
Can you use this docker-compose file?
version: "3"
volumes:
postgres_app: ~
services:
# Create a service named db.
postgres:
image: "postgres"
environment:
POSTGRES_USER: robert
POSTGRES_PASSWORD: robert
POSTGRES_DB: "mydb"
volumes:
- "postgres_app:/var/lib/postgresql/data"
ports:
- "54320:5432"
restart: always
And this command docker-compose exec postgres psql -U robert -d mydb
I hope this will help!
On my computer i executed this file

How do I create a database within a docker container using only the docker-compose file?

I'm trying to create a database and connect to it within my container network. I don't want to have to ssh into a box to create users/databases etc, as this is not a scalable or easily distributable process.
This is what I have so far:
# docker-compose.yml
db:
image: postgres:9.4
volumes:
- ./db/init.sql:/docker-entrypoint-initdb/10-init.sql
environment:
- PGDATA=/tmp
- PGDATABASE=web
- PGUSER=docker
- PGPASSWORD=password
This is my init.sql file:
CREATE DATABASE web;
CREATE USER docker WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE web TO docker;
When I start up the container and try to connect to it, I get this error:
db_1 | FATAL: role "docker" does not exist
db_1 | done
db_1 | server started
db_1 | FATAL: database "web" does not exist
db_1 | psql: FATAL: database "web" does not exist
The first time this happened, I tried to create a role like this:
CREATE ROLE docker with SUPERUSER PASSWORD password;
GRANT web TO docker;
But it did not have any effect. To make matters even more confusing, when I use node-postgres to connect to the db, I get this error:
Error: connect ECONNREFUSED
But how can the connection be refused if the db service isnt even up??
In a nutshell, these are the questions I'm trying to solve:
How can I create a database using only the files in my project (i.e. no manual commands)?
How do I create a user/role using only the files in my project?
How do I connect to this database?
Thank you in advance.
How can I create a database using only the files in my project (i.e.
no manual commands)?
The minimal docker-compose.yml config for you defined user and database is:
postgres:
image: postgres:9.4
environment:
- POSTGRES_DB=web
- POSTGRES_USER=myuser
How do I create a user/role using only the files in my project?
To execute scripts on database initialization take a look at the official docs for initdb.
To get you started with a quick and dirty solution create a new file e.g. init_conf.sh in the same directory as your docker-compose.yml:
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
CREATE ROLE docker with SUPERUSER PASSWORD 'password';
EOSQL
And add the volumes directive to your docker-compose.yml.
volumes:
- .:/docker-entrypoint-initdb.d
Recreate your container because otherwise, you wouldn't trigger a new database initialization. That means, docker stop and docker rm the old one first before executing docker-compose up again. STDOUT gives you now some information about our newly introduced script.
How do I connect to this database?
To connect to your database with docker exec via the terminal:
docker exec -ti folder_postgres_1 psql -U myuser -d web
A docker-compose.yml in one of my production environments looks like the following:
services:
postgres:
logging: &logging
driver: json-file
options:
max-size: "10m"
max-file: "5"
build: ./docker/postgres # path to custom Dockerfile
volumes:
- postgres_data:/var/lib/postgresql/data
- postgres_backup:/backups
env_file: .env
restart: always
# ... other services like web, celery, redis, etc.
Dockerfile:
FROM postgres:latest
# ...
COPY *.sh /docker-entrypoint-initdb.d/
# ...
The environment variable you are using are wrong. Try this
version: '3.3'
services:
db:
image: postgres:9.4
restart: always
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=password
- POSTGRES_DB=web
volumes:
- db_data:/var/lib/postgresql/data
# optional port
ports: ["5555:5432"]
volumes:
db_data:
then from any other docker-compose service you can access the DB at db:5432 and from your host machine you can access postgres on localhost:5555 if you also add the ports

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