Why docker-compose fail and docker run success with postgresql? - postgresql

I am tring to start a postgreSQL docker container with my mac; i use OSX 10.11.16 El Capitan with Docker Toolbox 19.03.01.
If i run:
docker run --name my_postgres -v my_dbdata:/var/lib/postgresql/data -p 54320:5432 postgres:11
all was done and i get:
my_postgres | 2019-09-17 04:51:48.908 UTC [41] LOG: database system is ready to accept connections
but if i use an .yml file like this one:
docker-compose.yml:
version: "3"
services:
db:
image: "postgres:11"
container_name: "my_postgres"
ports:
- "54320:5432"
volumes:
- my_dbdata:/var/lib/postgresql/data
volumes:
my_dbdata:
and run
docker-compose up
i get instead:
my_postgres | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
my_postgres |
my_postgres | 2019-09-17 04:51:49.009 UTC [41] LOG: received fast shutdown request
my_postgres | 2019-09-17 04:51:49.011 UTC [41] LOG: aborting any active transactions
my_postgres | waiting for server to shut down....2019-09-17 04:51:49.087 UTC [41] LOG: background worker "logical replication launcher" (PID 48) exited with exit code 1
my_postgres | 2019-09-17 04:51:49.091 UTC [43] LOG: shutting down
my_postgres | 2019-09-17 04:51:49.145 UTC [41] LOG: database system is shut down
Why the same think with docker-compose fail?
So many thanks in advance

Try the below one it worked for me
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: mypassword
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- 5432:5432
Then use docker-compose up to get the logs after using the previous command use
docker-compose logs -f

If you are trying to access and existing volume on the host machine, you need to specify that the volume was created outside the Compose file with the external keyword like this:
version: "3.7"
services:
db:
image: postgres
volumes:
- data:/var/lib/postgresql/data
volumes:
data:
external: true
I took the example from the Compose file reference https://docs.docker.com/compose/compose-file/.
Also double check the contents of your external volume between runs, to see if it was overriden.
Please also double check your quotes, you don't need to put the image name in quotes, but I don't think that's the issue.

The my_dbdata named volume is not the same for the two cases.
docker run creates a volume named my_dbdata, instead docker-compose creates by default a volume called <dir>_my_dbdata
Run docker volume to list the volumes:
docker volume ls |grep my_dbdata
I suspect the volume created by docker-compose has issues and as a consequence postgres doesn't start correctly. The initialization of the database in the my_postgres container is done only once.
Try to remove the container and the volume created by docker-compose:
docker rm my_postgres
docker volume rm <dir>_my_dbdata
Hope it helps

Related

Docker compose with Postresql runs into a zero-length delimited identifier

I'm trying to run postresql with docker compose
version: "3.8"
services:
db:
image: postgres:14.1-alpine
restart: always
environment:
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=test
ports:
- "5432:5432"
volumes:
- db:/var/lib/postgresql/data
volumes:
db:
driver: local
When I then try to start it with docker compose up I get the following error:
db-1 | performing post-bootstrap initialization ... 2022-09-19 12:45:21.913 UTC [30] FATAL: zero-length delimited identifier at or near """" at character 12
db-1 | 2022-09-19 12:45:21.913 UTC [30] STATEMENT: ALTER USER ""test"" WITH PASSWORD E'"test"';
db-1 |
db-1 | child process exited with exit code 1
I have experimented with single quotes, double quotes, no quotes for the environment variables, but the issue persists. My understanding is that nothing that isn't in the docker-compose.yml can be affecting the database. There is no postgres running locally when I try to start this. What is causing the issue?
Edit: Docker compose version is
➜ ~ docker compose version
Docker Compose version v2.10.2
Edit: Added the output of a non-printable character checker

Docker Postgres PGAdmin4 MacOS - pgadmin4 does not show docker Postgres volume data after restart

I recently had to restart my machine, thus shut down my docker container and server connections. After restart PGAdmin4 no longer shows docker volume data, however my application is still able to access the docker volume data. I'm able to log in to my db as user postgres, though no tables show in the db. PGAdmin4 on 5050:5050, Postgres on 5432:5432, app on 5000:5000. I'm able to log into the db via PGAdmin while the container is paused, which wasn't the case before, finding it hard to understand why.
I've got a docker-compose file like so:
version: "3.7"
services:
postgres:
container_name: postgres
restart: always
image: postgres:12.2
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
- flask-app-db:/var/lib/postgresql/data
ports:
- 5432:5432
networks:
- pgnetwork
pgadmin4:
container_name: pgadmin4
restart: always
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL}
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD}
- SERVER_PORT=${SERVER_PORT}
volumes:
- pga4:/var/lib/pgadmin
ports:
- 5050:5050
networks:
- pgnetwork
dplio:
container_name: dplio
restart: always
build:
context: .
dockerfile: Dockerfile
environment:
# MY_ENV_VARS
ports:
- 5000:5000
volumes:
- .:/dplio
networks:
- pgnetwork
depends_on:
- postgres
networks:
pgnetwork:
driver: bridge
volumes:
flask-app-db:
name: flask-app-db
pga4:
name: pga4
My terminal output when the containers are run:
Starting postgres ... done
Starting pgadmin4 ... done
Starting dplio ... done
Attaching to pgadmin4, postgres, dplio
postgres |
postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres |
postgres | 2020-03-17 11:18:55.599 UTC [1] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres | 2020-03-17 11:18:55.599 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres | 2020-03-17 11:18:55.600 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres | 2020-03-17 11:18:55.604 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres | 2020-03-17 11:18:55.619 UTC [26] LOG: database system was shut down at 2020-03-17 10:41:43 UTC
postgres | 2020-03-17 11:18:55.623 UTC [1] LOG: database system is ready to accept connections
pgadmin4 | [2020-03-17 11:18:56 +0000] [1] [INFO] Starting gunicorn 19.9.0
pgadmin4 | [2020-03-17 11:18:56 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
pgadmin4 | [2020-03-17 11:18:56 +0000] [1] [INFO] Using worker: threads
pgadmin4 | [2020-03-17 11:18:56 +0000] [80] [INFO] Booting worker with pid: 80
dplio | * Serving Flask app "app" (lazy loading)
dplio | * Environment: docker
dplio | * Debug mode: on
dplio | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
dplio | * Restarting with stat
dplio | * Debugger is active!
dplio | * Debugger PIN: 209-285-043
I thought to try a pg_restore from the docker volume to host but that doesn't seem to make sense as a potential solution, I think it's a connection issue between the docker container and my host machine, not a data loss problem.
I set PGAdmin4 port to 5050 in the browser, though it worked fine without that before I restarted.
I tried visiting http://localhost:5050/?key=xxxxxxx http://0.0.0.0:5050/?key=xxxxxxx and http://1270.0.0.1:5050/?key=xxxxxxx, the key taken from ~/.pgAdmin4.XXXXXXXXXXXXXXXXXX.addr same issue.
I've tried removing my docker app image and rebuilding it, too.
Can anyone let me know what I'm missing? A step by step for shutting down server/container/network connections and restarting would be super helpful!
If you need more info let me know.
Thanks!

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

dockerized postgres - lock file "postmaster.pid" is empty

I'm experiencing issues using the official Postgres docker image, something that never happened on this machine.
I'm using that through docker-compose but when running docker-compose up, the db container does not start, here's the error:
db_1 | 2019-04-19 22:20:27.180 UTC [1] FATAL: lock file "postmaster.pid" is empty
db_1 | 2019-04-19 22:20:27.180 UTC [1] HINT: Either another server is starting, or the lock file is the remnant of a previous server startup crash.
I tried several times to remove the image so it then will be re-pulled, but hasn't solved the problem.
I'm using Docker for Mac 2.0.0.3 (31259)
here's the docker-compose.yml:
version: '2'
services:
web:
build: .
entrypoint: sh -c "python3 /var/www/my_project/manage.py migrate --noinput &&
python3 /var/www/my_project/manage.py runserver 0.0.0.0:8000"
restart: on-failure
ports:
- "9001:8000"
volumes:
- .:/var/www/my_project
depends_on:
- db
env_file:
- ./.env
environment:
- DB_SERVER=db
- DB_NAME=postgres
- DB_USER=postgres
db:
ports:
- "9432:5432"
image: postgres
volumes:
- database:/var/lib/postgresql
volumes:
database:
Any help on this?
You can try to remove the pid file manually from the volume mounted by the container. Something like this:
Search the volume:
docker inspect container-name
Search for the directory where the volume is stored:
...
"Mounts": [
{
"Type": "volume",
"Name": "9009b4e54686dc36f797a15fd8fc09213f3e8949583b823525881c010f2fe347",
"Source": "/var/lib/docker/volumes/9009b4e54686dc36f797a15fd8fc09213f3e8949583b823525881c010f2fe347/_data",
Now delete the pid file:
rm /var/lib/docker/volumes/9009b4e54686dc36f797a15fd8fc09213f3e8949583b823525881c010f2fe347/_data/
After this you might get this error message:
lock file "/var/run/postgresql/.s.PGSQL.5432.lock" is empty
This can be fixed by overwriting that file with a file containing a number:
echo 12345 > bla
docker cp bla container-name:/var/run/postgresql/.s.PGSQL.5432.lock
docker restart container-name
It could happen because you are starting the PostgreSQL server when you are still running your PostgreSQL server.
Try
pg_ctl stop
then
pg_ctl start
or
pg_ctl restart

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