Postgres shuts down immediately when started with docker-compose - postgresql

Postgres shuts down immediately when started with docker-compose. The yaml file used is below
version: '2'
services:
postgres:
image: postgres:9.5
container_name: local-postgres9.5
ports:
- "5432:5432"
The log when docker-compose up command is executed
Creating local-postgres9.5
Attaching to local-postgres9.5
local-postgres9.5 | The files belonging to this database system will be owned by user "postgres".
local-postgres9.5 | This user must also own the server process.
local-postgres9.5 |
local-postgres9.5 | The database cluster will be initialized with locale "en_US.utf8".
local-postgres9.5 | The default database encoding has accordingly been set to "UTF8".
local-postgres9.5 | The default text search configuration will be set to "english".
local-postgres9.5 |
local-postgres9.5 | Data page checksums are disabled.
local-postgres9.5 |
local-postgres9.5 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
local-postgres9.5 | creating subdirectories ... ok
local-postgres9.5 | selecting default max_connections ... 100
local-postgres9.5 | selecting default shared_buffers ... 128MB
local-postgres9.5 | selecting dynamic shared memory implementation ... posix
local-postgres9.5 | creating configuration files ... ok
local-postgres9.5 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
local-postgres9.5 | initializing pg_authid ... ok
local-postgres9.5 | initializing dependencies ... ok
local-postgres9.5 | creating system views ... ok
local-postgres9.5 | loading system objects' descriptions ... ok
local-postgres9.5 | creating collations ... ok
local-postgres9.5 | creating conversions ... ok
local-postgres9.5 | creating dictionaries ... ok
local-postgres9.5 | setting privileges on built-in objects ... ok
local-postgres9.5 | creating information schema ... ok
local-postgres9.5 | loading PL/pgSQL server-side language ... ok
local-postgres9.5 | vacuuming database template1 ... ok
local-postgres9.5 | copying template1 to template0 ... ok
local-postgres9.5 | copying template1 to postgres ... ok
local-postgres9.5 | syncing data to disk ... ok
local-postgres9.5 |
local-postgres9.5 | WARNING: enabling "trust" authentication for local connections
local-postgres9.5 | You can change this by editing pg_hba.conf or using the option -A, or
local-postgres9.5 | --auth-local and --auth-host, the next time you run initdb.
local-postgres9.5 |
local-postgres9.5 | Success. You can now start the database server using:
local-postgres9.5 |
local-postgres9.5 | pg_ctl -D /var/lib/postgresql/data -l logfile start
local-postgres9.5 |
local-postgres9.5 | ****************************************************
local-postgres9.5 | WARNING: No password has been set for the database.
local-postgres9.5 | This will allow anyone with access to the
local-postgres9.5 | Postgres port to access your database. In
local-postgres9.5 | Docker's default configuration, this is
local-postgres9.5 | effectively any other container on the same
local-postgres9.5 | system.
local-postgres9.5 |
local-postgres9.5 | Use "-e POSTGRES_PASSWORD=password" to set
local-postgres9.5 | it in "docker run".
local-postgres9.5 | ****************************************************
local-postgres9.5 | waiting for server to start....LOG: database system was shut down at 2016-05-16 16:51:54 UTC
local-postgres9.5 | LOG: MultiXact member wraparound protections are now enabled
local-postgres9.5 | LOG: database system is ready to accept connections
local-postgres9.5 | LOG: autovacuum launcher started
local-postgres9.5 | done
local-postgres9.5 | server started
local-postgres9.5 | ALTER ROLE
local-postgres9.5 |
local-postgres9.5 |
local-postgres9.5 | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
local-postgres9.5 |
local-postgres9.5 | LOG: received fast shutdown request
local-postgres9.5 | LOG: aborting any active transactions
local-postgres9.5 | LOG: autovacuum launcher shutting down
local-postgres9.5 | LOG: shutting down
local-postgres9.5 | waiting for server to shut down....LOG: database system is shut down
local-postgres9.5 | done
local-postgres9.5 | server stopped
local-postgres9.5 |
local-postgres9.5 | PostgreSQL init process complete; ready for start up.
local-postgres9.5 |
local-postgres9.5 | LOG: database system was shut down at 2016-05-16 16:51:55 UTC
local-postgres9.5 | LOG: MultiXact member wraparound protections are now enabled
local-postgres9.5 | LOG: database system is ready to accept connections
local-postgres9.5 | LOG: autovacuum launcher started
Postgres seems to work fine when a container is started using the same image with docker run
docker run --name local-postgres9.5 -p 5432:5432 postgres:9.5

If you look at your log output, the following lines appear towards the end:
local-postgres9.5 | server stopped
local-postgres9.5 |
local-postgres9.5 | PostgreSQL init process complete; ready for start up.
Apparently, stopping and restarting the Postgres server is part of the initialisation process. In fact, the second-to-last line says
local-postgres9.5 | LOG: database system is ready to accept connections.

I'm using the same Postgres docker image version of yours (9.5) and I was running into the same problem.
The first time the container is created, Postgres run a series of commands and in the end, it just sends a shutdown signal and the server becomes unresponsive (but it works the second time onwards).
After several trials, I figured that once I tried to connect to the server, before it was ready to accept connections, PostgresDB would shutdown unexpectedly, and the same would happen for any client trying to connect remotely (outside the container).
I came across this error in the following scenario - I have two containers: one for the PostgresDB itself and other containing a Postgres client (psql) that tries to connect to the first container to create some users, databases and run a liquibase script that creates all the schemas.
At first, I was using a loop in my bash script checking if the server was available every 5 seconds, then after the first attempt, Postgres issued a signal to shutdown and becomes unresponsive.
I could get rid of this error by adding a healthcheck in my docker-compose.yml:
Checkout the CHANGE 1 and CHANGE 2 comments below
version: '3.9'
services:
postgres-db:
container_name: ${POSTGRES_HOST}
image: postgres:9.5
restart: always
ports:
- "5432:5432"
command: postgres
expose:
- 5432
environment:
POSTGRES_USER: "${POSTGRES_USER}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
POSTGRES_DB: "${POSTGRES_DB}"
#this ENV variable is only required for the healthcheck section - if you don't specify it, the check command will fail stating the root user doesn't exist in posgres
PGUSER: "postgres"
healthcheck:
#CHANGE 1: this command checks if the database is ready, right on the source db server
test: [ "CMD-SHELL", "pg_isready" ]
interval: 5s
timeout: 5s
retries: 5
liquibase:
container_name: liquibase-schema-config
image: company/liquibase
build:
context: ./liquibase
environment:
- PGPASSWORD=${POSTGRES_PASSWORD}
- PGPORT=${POSTGRES_PORT}
- PGHOST=${POSTGRES_HOST}
- PGUSER=${POSTGRES_USER}
- PGDATABASE=${POSTGRES_DB}
- JDBC_URL=jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/
- LIQUIBASE_HOME=${LIQUIBASE_HOME}
depends_on:
#CHANGE 2: it prevents issuing a request while the server is starting to depend on the healthy status of postgres-db
postgres-db:
condition: service_healthy
EDIT: There was another issue preventing Docker from accessing VPN protected sites (and even internet sites) while using Cisco AnyConnect on Linux (it doesn't happen on MacOS). To get around this unwanted behavior I installed openconnect sudo apt install openconnect on my Ubuntu and stopped using AnyConnect.
Hope it helps!

I tried your docker-compose and the service seems running in the container:
root#0afe99de0f0b:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
postgres 1 0.5 0.8 227148 16128 ? Ss 14:42 0:00 postgres
postgres 74 0.0 0.0 227148 1768 ? Ss 14:42 0:00 postgres: checkpointer process
postgres 75 0.0 0.0 227148 1772 ? Ss 14:42 0:00 postgres: writer process
postgres 76 0.0 0.0 227148 1768 ? Ss 14:42 0:00 postgres: wal writer process
postgres 77 0.0 0.1 227576 2720 ? Ss 14:42 0:00 postgres: autovacuum launcher process
postgres 78 0.0 0.0 82132 1888 ? Ss 14:42 0:00 postgres: stats collector process
root 79 2.0 0.0 21820 1984 ? Ss 14:42 0:00 /bin/bash
root 84 0.0 0.0 19092 1296 ? R+ 14:42 0:00 ps aux
Anyway, for my project I use another image for postgresql: https://github.com/sameersbn/docker-postgresql. This one works fine.

Add password under postgres service in docker-compose.yml as given in the screenshot. Thank you.
click to see the screenshot
version: '3'
services:
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=postgres_password

Related

Docker postgres 9.4 password authentication failed for user "postgres"

I am running very basic docker-compose with following docker-compose.yml and getting following error.
docker-compose.yml
services:
redis:
image: redis
db:
image: postgres:9.4
environment:
- POSTGRES_USER=udcoker
- POSTGRES_PASSWORD=udcoker
- POSTGRES_DB=docker
vote:
image: voting-app
ports:
- 5000:80
depends_on:
- redis
worker:
image: worker-app
depends_on:
- redis
- db
environment:
- POSTGRES_USER=udcoker
- POSTGRES_PASSWORD=udcoker
- POSTGRES_DB=docker
result:
image: result-app
ports:
- 5001:80
depends_on:
- db
environment:
- POSTGRES_USER=udcoker
- POSTGRES_PASSWORD=udcoker
- POSTGRES_DB=docker
Error generated,
db_1 | vacuuming database template1 ... ok
db_1 | copying template1 to template0 ... ok
db_1 | copying template1 to postgres ... ok
db_1 | syncing data to disk ...
db_1 | WARNING: enabling "trust" authentication for local connections
db_1 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --auth-local and --auth-host, the next time you run initdb.
db_1 | ok
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | postgres -D /var/lib/postgresql/data
db_1 | or
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 | waiting for server to start....LOG: database system was shut down at 2021-03-03 12:45:13 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
worker_1 | Waiting for db
result_1 | Waiting for db
db_1 | done
db_1 | server started
db_1 | CREATE DATABASE
db_1 |
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | LOG: received fast shutdown request
db_1 | LOG: aborting any active transactions
db_1 | LOG: autovacuum launcher shutting down
db_1 | waiting for server to shut down....LOG: shutting down
db_1 | LOG: database system is shut down
worker_1 | Waiting for db
result_1 | Waiting for db
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | LOG: database system was shut down at 2021-03-03 12:45:15 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
db_1 | FATAL: password authentication failed for user "postgres"
db_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all all md5"
db_1 | FATAL: password authentication failed for user "postgres"
db_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all all md5"
I checked on various stackoverflow existing questions but none of them are rectifying actual issue. Some suggesting to run postgres docker and bash it to update the password but its a workaround. Anyone has any concrete solution to this?
I figured it out finally and would be good to share what mistake I had done.
I changed the database configuration by providing environment variable in the docker-compose.yml file in db service as mentioned in the question already,
environment:
- POSTGRES_USER=udcoker
- POSTGRES_PASSWORD=udcoker
- POSTGRES_DB=docker
But the same needs to be updated in all the other custom services (open source github) which are trying to connect to this depende-on postgres database with same user/password/database which was not done.
However the error given by docker-compose is not explaining the situation clearly.
In All, I updated the applications with above username/password/database in connection property and the containers are up.

docker-compose postgres restart after running scripts in docker-entrypoint-initdb.d

I have a simple Docker Compose file to initialize a Postgres DB Instance:
version: '3.8'
services:
my-database:
container_name: my-database
image: library/postgres:13.1
volumes:
- ./db/init-my-database.sql:/docker-entrypoint-initdb.d/init-db-01.sql
environment:
- POSTGRES_DB=my-database
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=password
ports:
- 10040:5432
restart: always
networks:
- app-network
And my script init-my-database.sql looks like this:
DROP DATABASE IF EXISTS myschema;
CREATE DATABASE myschema;
-- Make sure we're using our `myschema` database
\c myschema;
CREATE SEQUENCE IF NOT EXISTS recipient_seq
START WITH 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
CREATE TABLE IF NOT EXISTS recipient
(
id BIGINT NOT NULL,
recipient_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
middle_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
CONSTRAINT recipient_pk PRIMARY KEY (id)
);
When I tail the Docker logs I do see that the initialization script is being called. However, towards the end the database is shutdown and restarted --> "received fast shutdown request"
Why is the database restarted at the end? As the database and table created in the script is not available anymore.
Complete Docker Logs -->
my-database | The files belonging to this database system will be owned by user "postgres".
my-database | This user must also own the server process.
my-database |
my-database | The database cluster will be initialized with locale "en_US.utf8".
my-database | The default database encoding has accordingly been set to "UTF8".
my-database | The default text search configuration will be set to "english".
my-database |
my-database | Data page checksums are disabled.
my-database |
my-database | fixing permissions on existing directory /var/lib/postgresql/data ... ok
my-database | creating subdirectories ... ok
my-database | selecting dynamic shared memory implementation ... posix
my-database | selecting default max_connections ... 100
my-database | selecting default shared_buffers ... 128MB
my-database | selecting default time zone ... Etc/UTC
my-database | creating configuration files ... ok
my-database | running bootstrap script ... ok
my-database | performing post-bootstrap initialization ... ok
my-database | syncing data to disk ... ok
my-database |
my-database | initdb: warning: enabling "trust" authentication for local connections
my-database | You can change this by editing pg_hba.conf or using the option -A, or
my-database | --auth-local and --auth-host, the next time you run initdb.
my-database |
my-database | Success. You can now start the database server using:
my-database |
my-database | pg_ctl -D /var/lib/postgresql/data -l logfile start
my-database |
my-database | waiting for server to start....2020-12-22 23:42:50.083 UTC [45] LOG: starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
my-database | 2020-12-22 23:42:50.085 UTC [45] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my-database | 2020-12-22 23:42:50.091 UTC [46] LOG: database system was shut down at 2020-12-22 23:42:49 UTC
my-database | 2020-12-22 23:42:50.096 UTC [45] LOG: database system is ready to accept connections
my-database | done
my-database | server started
my-database | CREATE DATABASE
my-database |
my-database |
my-database | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init-db-01.sql
my-database | psql:/docker-entrypoint-initdb.d/init-db-01.sql:1: NOTICE: database "mergeletter" does not exist, skipping
my-database | DROP DATABASE
my-database | CREATE DATABASE
my-database | You are now connected to database "mergeletter" as user "admin".
my-database | CREATE SEQUENCE
my-database | CREATE TABLE
my-database |
my-database |
my-database | 2020-12-22 23:42:50.515 UTC [45] LOG: received fast shutdown request
my-database | waiting for server to shut down....2020-12-22 23:42:50.516 UTC [45] LOG: aborting any active transactions
my-database | 2020-12-22 23:42:50.521 UTC [45] LOG: background worker "logical replication launcher" (PID 52) exited with exit code 1
my-database | 2020-12-22 23:42:50.521 UTC [47] LOG: shutting down
my-database | 2020-12-22 23:42:50.541 UTC [45] LOG: database system is shut down
my-database | done
my-database | server stopped
my-database |
my-database | PostgreSQL init process complete; ready for start up.
my-database |
my-database | 2020-12-22 23:42:50.648 UTC [1] LOG: starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
my-database | 2020-12-22 23:42:50.649 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
my-database | 2020-12-22 23:42:50.649 UTC [1] LOG: listening on IPv6 address "::", port 5432
my-database | 2020-12-22 23:42:50.652 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my-database | 2020-12-22 23:42:50.657 UTC [73] LOG: database system was shut down at 2020-12-22 23:42:50 UTC
my-database | 2020-12-22 23:42:50.663 UTC [1] LOG: database system is ready to accept connections
Put simply, this happens on purpose; the author does it as part of the initialization.
It looks like some answers can be found in the image's entrypoint shell script:
_main() {
# if first arg looks like a flag, assume we want to run postgres server
if [ "${1:0:1}" = '-' ]; then
set -- postgres "$#"
fi
if [ "$1" = 'postgres' ] && ! _pg_want_help "$#"; then
docker_setup_env
# setup data directories and permissions (when run as root)
docker_create_db_directories
if [ "$(id -u)" = '0' ]; then
# then restart script as postgres user
exec gosu postgres "$BASH_SOURCE" "$#"
fi
# only run initialization on an empty data directory
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
docker_verify_minimum_env
# check dir permissions to reduce likelihood of half-initialized database
ls /docker-entrypoint-initdb.d/ > /dev/null
docker_init_database_dir
pg_setup_hba_conf
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
docker_temp_server_start "$#"
docker_setup_db
docker_process_init_files /docker-entrypoint-initdb.d/*
docker_temp_server_stop
unset PGPASSWORD
echo
echo 'PostgreSQL init process complete; ready for start up.'
echo
else
echo
echo 'PostgreSQL Database directory appears to contain a database; Skipping initialization'
echo
fi
fi
exec "$#"
}
As for "why?" I think it's because of desire to run as a less-privileged user.
You can "solve" the problem by specifying a volume in the Compose file like so:
volumes:
- ./data/pgsql:/var/lib/postgresql/data
Then, it will skip the routine to ensure DATABASE_ALREADY_EXISTS.
Or, if that's not helpful--you can dig into the entrypoint script a bit more.
Thanks for the above tip from #TorEHagermann (https://stackoverflow.com/a/65417566/5631863) I was able to resolve the problem.
The solution was to include the following:
volumes:
- ./data/pgsql:/var/lib/postgresql/data
Also, in my case, I needed to clear and refresh the data every time I started the container. So, I included the following in my shell script to delete the drive /data/pgsql:
rm -rf data
With that change, my docker-compose file looks like:
version: '3.8'
services:
my-database:
container_name: my-database
image: library/postgres:13.1
volumes:
- ./db/init-my-database.sql:/docker-entrypoint-initdb.d/init-db-01.sql
- ./data/pgsql:/var/lib/postgresql/data
environment:
- POSTGRES_DB=my-database
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=password
ports:
- 10040:5432
restart: always
networks:
- app-network
As mentioned above, the restart is intentional. The reason is that changes in some of the database server settings need restart to take effect. The entrypoint init script is a convenient place for the queries that change these settings.
e.g.
ALTER SYSTEM SET max_connections = 300;

Docker | Postgres Database is uninitialized and superuser password is not specified

I am using docker-compose.yml to create multiple running containers but failing to start Postgres docker server, with following logs and yes I have searched many related SO posts, but they didn't helped me out.
Creating network "complex_default" with the default driver
Creating complex_server_1 ... done
Creating complex_redis_1 ... done
Creating complex_postgres_1 ... done
Attaching to complex_postgres_1, complex_redis_1, complex_server_1
postgres_1 | Error: Database is uninitialized and superuser password is not specified.
postgres_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1 |
postgres_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1 | connections without a password. This is *not* recommended.
postgres_1 |
postgres_1 | See PostgreSQL documentation about "trust":
postgres_1 | https://www.postgresql.org/docs/current/auth-trust.html
complex_postgres_1 exited with code 1
below is my docker-compose configuration:
version: '3'
services:
postgres:
image: 'postgres:11-alpine'
redis:
image: 'redis:latest'
server:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /app/node_modules
- ./server:/app
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
as well as package.json inside server directory is following:
{
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.4",
"express": "^4.16.3",
"nodemon": "^2.0.4",
"pg": "7.4.3",
"redis": "^2.8.0"
},
"scripts": {
"dev": "nodemon",
"start": "node index.js"
}
}
also for better consideration, I have attached my hands-on project structure:
A year ago it were actually working fine, Does anyone have any idea, what's going wrong here inside my docker-compose file now.
A year ago it were actually working fine, Does anyone have any idea, what's going wrong here inside my docker-compose file now.
Seems like you pulled the fresh image, where in the new image you should specify Postgres user password. You can look into Dockerhub, the image is update one month ago
postgress-11-alpine
db:
image: postgres:11-alpine
restart: always
environment:
POSTGRES_PASSWORD: example
As the error message is self expalinatory
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
or POSTGRES_HOST_AUTH_METHOD=trust use this which is not recommended.
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
POSTGRES_PASSWORD
This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.
Environment Variables
#Adiii yes, you are nearly right, so I have to explicitly mentioned the environment also for the postgres image but with no db parent tag.
So here, I am explicitly mentioning the docker-compose.yaml config to help others for better understanding, also now I am using recent stable postgres image version 12-alpine, currently latest is postgres:12.3
version: '3'
services:
postgres:
image: 'postgres:12-alpine'
environment:
POSTGRES_PASSWORD: postgres_password
redis:
image: 'redis:latest'
server:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /app/node_modules
- ./server:/app
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
and so after docker-compose up the creating and running logs were as following:
PS E:\docker\complex> docker-compose up
Creating network "complex_default" with the default driver
Creating complex_postgres_1 ... done
Creating complex_redis_1 ... done
Creating complex_server_1 ... done
Attaching to complex_redis_1, complex_postgres_1, complex_server_1
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
postgres_1 |
postgres_1 | The database cluster will be initialized with locale "en_US.utf8".
postgres_1 | The default database encoding has accordingly been set to "UTF8".
postgres_1 | The default text search configuration will be set to "english".
postgres_1 |
postgres_1 | Data page checksums are disabled.
postgres_1 |
postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1 | creating subdirectories ... ok
postgres_1 | selecting dynamic shared memory implementation ... posix
postgres_1 | selecting default max_connections ... 100
postgres_1 | selecting default shared_buffers ... 128MB
postgres_1 | selecting default time zone ... UTC
redis_1 | 1:C 05 Aug 2020 14:24:48.692 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 05 Aug 2020 14:24:48.692 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 05 Aug 2020 14:24:48.692 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
postgres_1 | creating configuration files ... ok
redis_1 | 1:M 05 Aug 2020 14:24:48.693 * Running mode=standalone, port=6379.
redis_1 | 1:M 05 Aug 2020 14:24:48.693 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 05 Aug 2020 14:24:48.694 # Server initialized
redis_1 | 1:M 05 Aug 2020 14:24:48.694 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 05 Aug 2020 14:24:48.694 * Ready to accept connections
postgres_1 | running bootstrap script ... ok
server_1 |
server_1 | > # dev /app
server_1 | > nodemon
server_1 |
postgres_1 | performing post-bootstrap initialization ... sh: locale: not found
postgres_1 | 2020-08-05 14:24:50.153 UTC [29] WARNING: no usable system locales were found
server_1 | [nodemon] 2.0.4
server_1 | [nodemon] to restart at any time, enter `rs`
server_1 | [nodemon] watching path(s): *.*
server_1 | [nodemon] watching extensions: js,mjs,json
server_1 | [nodemon] starting `node index.js`
postgres_1 | ok
server_1 | Listening
postgres_1 | syncing data to disk ... ok
postgres_1 |
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1 |
postgres_1 | initdb: warning: enabling "trust" authentication for local connections
postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1 | --auth-local and --auth-host, the next time you run initdb.
postgres_1 | waiting for server to start....2020-08-05 14:24:51.634 UTC [34] LOG: starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
postgres_1 | 2020-08-05 14:24:51.700 UTC [34] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-08-05 14:24:51.981 UTC [35] LOG: database system was shut down at 2020-08-05 14:24:50 UTC
postgres_1 | 2020-08-05 14:24:52.040 UTC [34] LOG: database system is ready to accept connections
postgres_1 | done
postgres_1 | server started
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1 |
postgres_1 | waiting for server to shut down....2020-08-05 14:24:52.121 UTC [34] LOG: received fast shutdown request
postgres_1 | 2020-08-05 14:24:52.186 UTC [34] LOG: aborting any active transactions
postgres_1 | 2020-08-05 14:24:52.188 UTC [34] LOG: background worker "logical replication launcher" (PID 41) exited with exit code 1
postgres_1 | 2020-08-05 14:24:52.188 UTC [36] LOG: shutting down
postgres_1 | 2020-08-05 14:24:52.669 UTC [34] LOG: database system is shut down
postgres_1 | done
postgres_1 | server stopped
postgres_1 |
postgres_1 | PostgreSQL init process complete; ready for start up.
postgres_1 |
postgres_1 | 2020-08-05 14:24:52.832 UTC [1] LOG: starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
postgres_1 | 2020-08-05 14:24:52.832 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-08-05 14:24:52.832 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-08-05 14:24:52.954 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-08-05 14:24:53.136 UTC [43] LOG: database system was shut down at 2020-08-05 14:24:52 UTC
postgres_1 | 2020-08-05 14:24:53.194 UTC [1] LOG: database system is ready to accept connections
Hope this would help manyone.
Adding on ArifMustafa's answer, This worked for me.
postgres:
image: 'postgres:12-alpine'
environment:
POSTGRES_PASSWORD: mypassword
expose:
- 5432
volumes:
- postgres_data:/var/lib/postgres/data/
volumes:
postgres_data:
In my case there was an error about the POSTGRES_PASSWORD (in docker-compose.yml) and the DATABASE settings for PASSWORD in the project_level settings.py file.
After providing a password in docker-compose.yml
db: # For the PostgreSQL database
image: postgres:11
environment:
- POSTGRES_PASSWORD=example
It is necessary to provide the same password to enable access to the POSTGRES database between the two containers (in my case the web application and the database it depended on). In the project_level settings.py:
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'example',
'HOST': 'db',
'PORT': 5432
}
}
To resolve the error when using the command,
docker pull postgres
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
or POSTGRES_HOST_AUTH_METHOD=trust use this which is not recommended.
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is not recommended.
Here comes the solution for this:
$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
The default postgres user and database are created in the entrypoint with initdb.
The postgres database is a default database meant for use by users, utilities and third party applications.
postgresql.org/docs

Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied

I have the following docker compose file:
version: "3"
services:
postgres:
image: postgres:11.2-alpine
environment:
POSTGRES_PASSWORD: root
POSTGRES_USER: root
ports:
- "5432:5432"
volumes:
- ./init-db/init-db.sql:/docker-entrypoint-initdb.d/init.sql
This is the init-db.sql:
CREATE TABLE users (
email VARCHAR(355) UNIQUE NOT NULL,
password VARCHAR(256) NOT NULL
);
CREATE TABLE products (
id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
price NUMERIC(6, 2) NOT NULL,
category INT NOT NULL
);
INSERT INTO users VALUES ('test#test.com', 'Test*123');
INSERT INTO products (title, price, category) VALUES ('Truco', 9.90, 13);
When I run docker-compose up, I'm getting this error:
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
/docker-entrypoint-initdb.d/init.sql: Permission denied
I already tried to:
chmod 777 on the sql file
chmod -x on the sql file
Run docker and docker-compose using sudo
Any idea?
I found the easy way to solve this...
You should use "build" way to create postgres service
And DO NOT setting the volume for init.sql, it will cause the permission problem.
postgres:
build: ./postgres
Create a Dockerfile for postgres like this
FROM postgres:12
COPY ./init.sql /docker-entrypoint-initdb.d/init.sql
CMD ["docker-entrypoint.sh", "postgres"]
Then it should works out.
Hope my answer would help you!
For me problem was in my machine. enabled SELinux access control, which did not allow for containers to expand files.
Solution, disable SELinux:
echo SELINUX=disabled > /etc/selinux/config
SELINUXTYPE=targeted >> /etc/selinux/config
setenforce 0
From this
I had a similar problem when using the ADD command.
When using ADD (eg to download a file) the default chmod value is 711. When using the COPY command the chmod will match the hosts chmod of the file where you copy it from. The solution is to set the permission before copying, or change them in your Dockerfile after they've been copied.
It looks like there will finally be a "COPY --chmod 775" flag available in the upcoming docker 20 which will make this easier.
https://github.com/moby/moby/issues/34819
When the sql file in /docker-entrypoint-initdb.d/ has a permission of 775 then the file is run correctly.
You can test this within the image (override entrypoint to /bin/bash) using:
docker-entrypoint.sh postgres
I have same problem on my macOS, but it's OK on my ubuntu laptop.
I found the problem is that MacOS cannot let docker access the folder.
Following link may resolve your problem.
https://stackoverflow.com/a/58482702/10752354
Besides, in my case, I should add one line code in my init.sql file because the default database is "root", and I should change "root" database into "postgres" database, or in your case for another custom database instead of root.
> docker-compose exec db psql -U root
psql (13.0 (Debian 13.0-1.pgdg100+1))
Type "help" for help.
root=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privilege
s
-----------+-------+----------+------------+------------+-----------------
--
postgres | root | UTF8 | en_US.utf8 | en_US.utf8 |
root | root | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | root | UTF8 | en_US.utf8 | en_US.utf8 | =c/root
+
| | | | | root=CTc/root
template1 | root | UTF8 | en_US.utf8 | en_US.utf8 | =c/root
+
| | | | | root=CTc/root
(4 rows)
so I need to add \c postgres in my init.sql file:
\c postgres // for creating table in the database named 'postgres'
create table sample_table. ( ... )
I tried with the following compose file and it seems working as expected. are you sure form the path of the files you use?
version: "3"
services:
postgres:
image: postgres:11.2-alpine
environment:
POSTGRES_PASSWORD: root
POSTGRES_USER: root
ports:
- "5432:5432"
volumes:
- ./init-db.sql:/docker-entrypoint-initdb.d/init.sql
files structure
drwxr-xr-x 4 shihadeh 502596769 128B Feb 28 22:37 .
drwxr-xr-x 12 shihadeh 502596769 384B Feb 28 22:36 ..
-rw-r--r-- 1 shihadeh 502596769 244B Feb 28 22:37 docker-compose.yml
-rw-r--r-- 1 shihadeh 502596769 380B Feb 28 22:37 init-db.sql
output of docker-compose up
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
postgres_1 |
postgres_1 | The database cluster will be initialized with locale "en_US.utf8".
postgres_1 | The default database encoding has accordingly been set to "UTF8".
postgres_1 | The default text search configuration will be set to "english".
postgres_1 |
postgres_1 | Data page checksums are disabled.
postgres_1 |
postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1 | creating subdirectories ... ok
postgres_1 | selecting default max_connections ... 100
postgres_1 | selecting default shared_buffers ... 128MB
postgres_1 | selecting dynamic shared memory implementation ... posix
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... sh: locale: not found
postgres_1 | 2020-02-28 21:45:01.363 UTC [26] WARNING: no usable system locales were found
postgres_1 | ok
postgres_1 | syncing data to disk ... ok
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1 |
postgres_1 |
postgres_1 | WARNING: enabling "trust" authentication for local connections
postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1 | --auth-local and --auth-host, the next time you run initdb.
postgres_1 | waiting for server to start....2020-02-28 21:45:02.272 UTC [30] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-02-28 21:45:02.294 UTC [31] LOG: database system was shut down at 2020-02-28 21:45:01 UTC
postgres_1 | 2020-02-28 21:45:02.299 UTC [30] LOG: database system is ready to accept connections
postgres_1 | done
postgres_1 | server started
postgres_1 | CREATE DATABASE
postgres_1 |
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
postgres_1 | CREATE TABLE
postgres_1 | CREATE TABLE
postgres_1 | INSERT 0 1
postgres_1 | INSERT 0 1
postgres_1 |
postgres_1 |
postgres_1 | waiting for server to shut down....2020-02-28 21:45:02.776 UTC [30] LOG: received fast shutdown request
postgres_1 | 2020-02-28 21:45:02.779 UTC [30] LOG: aborting any active transactions
postgres_1 | 2020-02-28 21:45:02.781 UTC [30] LOG: background worker "logical replication launcher" (PID 37) exited with exit code 1
postgres_1 | 2020-02-28 21:45:02.781 UTC [32] LOG: shutting down
postgres_1 | 2020-02-28 21:45:02.826 UTC [30] LOG: database system is shut down
postgres_1 | done
postgres_1 | server stopped
postgres_1 |
postgres_1 | PostgreSQL init process complete; ready for start up.
postgres_1 |
postgres_1 | 2020-02-28 21:45:02.890 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-02-28 21:45:02.890 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-02-28 21:45:02.895 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-02-28 21:45:02.915 UTC [43] LOG: database system was shut down at 2020-02-28 21:45:02 UTC
postgres_1 | 2020-02-28 21:45:02.921 UTC [1] LOG: database system is ready to accept connections
Just adding my solution even when it is a little late:
My problem:
I am installing the source files from an udemy course:
https://github.com/rockthejvm/spark-essentials
We have 2 important things here:
Docker compose file: docker-compose.yml
Folder and sql file to do db initialization: ./sql/bd.sql
I downloaded the zip file to my Ubuntu 20.04
When doing docker-compose up, I got the error:
Attaching to postgres
postgres | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
postgres exited with code 2
SOLUTION
Before running doccker-compose up do:
run chmod 777 ON FOLDER called sql that contains file db.sql
chmod 777 sql
Explanation:
The folder called sql contains file db.sql to init the database from inside the docker container so, we need to give the permissions to run the file inside folde called sql.
I think you only need to do as below.
Give permisions to your folder:
chmod 777 init-db
be sure your sql file can be read by all users:
chmod 666 init-db.sql

docker-compose: console does not return to host when creating database container

I'm trying to create a postgresql docker container using docker-compose, but it seems that when I use a pre-ready postgresql image the console never return to the host, and if I hit Ctrl+C the container ends up being stoped. Here is what I got:
docker-compose.yml
version: '2'
services:
database:
container_name: database_server
build:
context: .
dockerfile: Dockerfile_database
image: database:tag
expose:
- "5432"
Dockerfile_database
FROM library/postgres
ADD init.sql /docker-entrypoint-initdb.d/
init.sql
CREATE USER usuario WITH PASSWORD 'senha';
CREATE DATABASE telesaude;
GRANT ALL PRIVILEGES ON DATABASE telesaude TO usuario;
and during the container creation the console shows some postgresql logs, such as this:
Building database
Step 1/2 : FROM library/postgres
---> 4023a747a01a
Step 2/2 : ADD init.sql /docker-entrypoint-initdb.d/
---> e264774d1f0b
Removing intermediate container 8df766b5a9f2
Successfully built e264774d1f0b
WARNING: Image for service database was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating database_server
Attaching to database_server
database_server | The files belonging to this database system will be owned by user "postgres".
database_server | This user must also own the server process.
database_server |
database_server | The database cluster will be initialized with locale "en_US.utf8".
database_server | The default database encoding has accordingly been set to "UTF8".
database_server | The default text search configuration will be set to "english".
database_server |
database_server | Data page checksums are disabled.
database_server |
database_server | fixing permissions on existing directory /var/lib/postgresql/data ... ok
database_server | creating subdirectories ... ok
database_server | selecting default max_connections ... 100
database_server | selecting default shared_buffers ... 128MB
database_server | selecting dynamic shared memory implementation ... posix
database_server | creating configuration files ... ok
database_server | running bootstrap script ... ok
database_server | performing post-bootstrap initialization ... ok
database_server | syncing data to disk ...
database_server | WARNING: enabling "trust" authentication for local connections
database_server | You can change this by editing pg_hba.conf or using the option -A, or
database_server | --auth-local and --auth-host, the next time you run initdb.
database_server | ok
database_server |
database_server | Success. You can now start the database server using:
database_server |
database_server | pg_ctl -D /var/lib/postgresql/data -l logfile start
database_server |
database_server | ****************************************************
database_server | WARNING: No password has been set for the database.
database_server | This will allow anyone with access to the
database_server | Postgres port to access your database. In
database_server | Docker's default configuration, this is
database_server | effectively any other container on the same
database_server | system.
database_server |
database_server | Use "-e POSTGRES_PASSWORD=password" to set
database_server | it in "docker run".
database_server | ****************************************************
database_server | waiting for server to start....LOG: database system was shut down at 2017-02-02 21:15:46 UTC
database_server | LOG: MultiXact member wraparound protections are now enabled
database_server | LOG: database system is ready to accept connections
database_server | LOG: autovacuum launcher started
database_server | done
database_server | server started
database_server | ALTER ROLE
database_server |
database_server |
database_server | /docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
database_server | CREATE ROLE
database_server | CREATE DATABASE
database_server | GRANT
database_server |
database_server |
database_server | LOG: received fast shutdown request
database_server | LOG: aborting any active transactions
database_server | waiting for server to shut down...LOG: autovacuum launcher shutting down
database_server | .LOG: shutting down
database_server | LOG: database system is shut down
database_server | done
database_server | server stopped
database_server |
database_server | PostgreSQL init process complete; ready for start up.
database_server |
database_server | LOG: database system was shut down at 2017-02-02 21:15:49 UTC
database_server | LOG: MultiXact member wraparound protections are now enabled
database_server | LOG: database system is ready to accept connections
database_server | LOG: autovacuum launcher started
(console never returns to host after this)
What should I do so the container keeps running/started and the console returns to host? I have to use docker-compose. Can't use docker run -d ...
To run the container in the background with compose, use the detach option:
docker-compose up -d