How to connect to postgres through docker-compose network? - postgresql

I use docker-compose and I try to connect to the postgres database from the web container.
I use this URI:
postgresql://hola:hola#postgres/holadb
I get this error:
Connection refused
Is the server running on host "postgres" (172.18.0.2) and accepting
TCP/IP connections on port 5432?
docker-compose.yml
version: '2'
services:
web:
restart: always
build: ./web
expose:
- "8000"
volumes:
- /usr/src/app/project/static
command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
depends_on:
- postgres
postgres:
image: postgres:9.6
ports:
- "5432:5432"
environment:
- POSTGRES_USER=hola
- POSTGRES_PASSWORD=hola
- POSTGRES_DB=holadb
volumes:
- ./data/postgres:/var/lib/postgresql/data
I remove ./data/postgres before building and running.
Logs
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
web_1 | [2017-06-03 16:54:14 +0000] [1] [INFO] Starting gunicorn 19.7.1
web_1 | [2017-06-03 16:54:14 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_1 | [2017-06-03 16:54:14 +0000] [1] [INFO] Using worker: sync
web_1 | [2017-06-03 16:54:14 +0000] [7] [INFO] Booting worker with pid: 7
web_1 | [2017-06-03 16:54:14 +0000] [8] [INFO] Booting worker with pid: 8
postgres_1 | performing post-bootstrap initialization ... ok
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 | 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 | waiting for server to start....LOG: database system was shut down at 2017-06-03 16:54:16 UTC
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: autovacuum launcher started
postgres_1 | done
postgres_1 | server started
postgres_1 | CREATE DATABASE
postgres_1 |
postgres_1 | CREATE ROLE
postgres_1 |
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1 |
postgres_1 | LOG: received fast shutdown request
postgres_1 | LOG: aborting any active transactions
postgres_1 | LOG: autovacuum launcher shutting down
postgres_1 | LOG: shutting down
postgres_1 | waiting for server to shut down....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 | LOG: database system was shut down at 2017-06-03 16:54:18 UTC
postgres_1 | LOG: MultiXact member wraparound protections are now enabled
postgres_1 | LOG: database system is ready to accept connections
postgres_1 | LOG: autovacuum launcher started
I don't understand why it does not work. Thank you in advance for your help.

The web container tries to connect while postgres is still initializing...
Waiting some delay solved my issue.
EDIT: I use Docker Compose Healthcheck to do this.

You need to setup a network to allow communication between containers. Something like this should work:
version: '2'
services:
web:
restart: always
build: ./web
expose:
- "8000"
volumes:
- /usr/src/app/project/static
command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
depends_on:
- postgres
networks: ['mynetwork']
postgres:
restart: always
build:
context: ./postgresql
volumes_from:
- data
ports:
- "5432:5432"
networks: ['mynetwork']
networks: {mynetwork: {}}
More info here and here.

Related

Why are my postgres user and volume not created by docker-compose?

I am having trouble composing a docker running postgres. I cannot get it to create the user postgres, nor do I see the volume I specify. I had this working before, I didn't think I changed anything, but obviously I did. In any case, here is what happens now:
Step 1: Clean environment of previous data:
docker stop $DOCKERID
docker rm $DOCKERID
sudo rm -rf .pgdata
Check that no volumes remain:
╰─ docker volume ls ─╯
DRIVER VOLUME NAME
Step 2: Create the data folder and then the container:
mkdir .pgdata
docker-compose -f docker-compose-pg-only.yml up &
docker-compose-pg-only.yml
version: '3.3'
services:
postgres:
image: postgres:13.2
restart: unless-stopped
environment:
POSTGRES_DB: mydb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: abc123
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
volumes:
-
type: bind
source: /path/to/postgres-docker-build/.pgdata
target: /var/lib/postgresql/data
networks:
- reference
networks:
reference:
The outpuut of docker-compose:
+] Running 1/1 ─╯
⠿ Container postgres-docker-build-db-1 Create... 0.1s
Attaching to postgres-docker-build-db-1
postgres-docker-build-db-1 | The files belonging to this database system will be owned by user "postgres".
postgres-docker-build-db-1 | This user must also own the server process.
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | The database cluster will be initialized with locale "en_US.utf8".
postgres-docker-build-db-1 | The default database encoding has accordingly been set to "UTF8".
postgres-docker-build-db-1 | The default text search configuration will be set to "english".
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | Data page checksums are disabled.
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | fixing permissions on existing directory /var/lib/postgresql/data/pgdata ... ok
postgres-docker-build-db-1 | creating subdirectories ... ok
postgres-docker-build-db-1 | selecting dynamic shared memory implementation ... posix
postgres-docker-build-db-1 | selecting default max_connections ... 100
postgres-docker-build-db-1 | selecting default shared_buffers ... 128MB
postgres-docker-build-db-1 | selecting default time zone ... UTC
postgres-docker-build-db-1 | creating configuration files ... ok
postgres-docker-build-db-1 | running bootstrap script ... ok
postgres-docker-build-db-1 | performing post-bootstrap initialization ... sh: locale: not found
postgres-docker-build-db-1 | 2022-06-25 17:25:25.749 UTC [31] WARNING: no usable system locales were found
postgres-docker-build-db-1 | ok
postgres-docker-build-db-1 | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
postgres-docker-build-db-1 | You can change this by editing pg_hba.conf or using the option -A, or
postgres-docker-build-db-1 | --auth-local and --auth-host, the next time you run initdb.
postgres-docker-build-db-1 | ok
postgres-docker-build-db-1 |
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | Success. You can now start the database server using:
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | pg_ctl -D /var/lib/postgresql/data/pgdata -l logfile start
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | waiting for server to start....2022-06-25 17:25:26.486 UTC [37] LOG: starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres-docker-build-db-1 | 2022-06-25 17:25:26.496 UTC [37] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-docker-build-db-1 | 2022-06-25 17:25:26.511 UTC [38] LOG: database system was shut down at 2022-06-25 17:25:26 UTC
postgres-docker-build-db-1 | 2022-06-25 17:25:26.517 UTC [37] LOG: database system is ready to accept connections
postgres-docker-build-db-1 | done
postgres-docker-build-db-1 | server started
postgres-docker-build-db-1 | CREATE DATABASE
postgres-docker-build-db-1 |
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | waiting for server to shut down....2022-06-25 17:25:26.709 UTC [37] LOG: received fast shutdown request
postgres-docker-build-db-1 | 2022-06-25 17:25:26.713 UTC [37] LOG: aborting any active transactions
postgres-docker-build-db-1 | 2022-06-25 17:25:26.714 UTC [37] LOG: background worker "logical replication launcher" (PID 44) exited with exit code 1
postgres-docker-build-db-1 | 2022-06-25 17:25:26.715 UTC [39] LOG: shutting down
postgres-docker-build-db-1 | 2022-06-25 17:25:26.740 UTC [37] LOG: database system is shut down
postgres-docker-build-db-1 | done
postgres-docker-build-db-1 | server stopped
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | PostgreSQL init process complete; ready for start up.
postgres-docker-build-db-1 |
postgres-docker-build-db-1 | 2022-06-25 17:25:26.840 UTC [1] LOG: starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres-docker-build-db-1 | 2022-06-25 17:25:26.840 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres-docker-build-db-1 | 2022-06-25 17:25:26.840 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres-docker-build-db-1 | 2022-06-25 17:25:26.848 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-docker-build-db-1 | 2022-06-25 17:25:26.858 UTC [51] LOG: database system was shut down at 2022-06-25 17:25:26 UTC
postgres-docker-build-db-1 | 2022-06-25 17:25:26.864 UTC [1] LOG: database system is ready to accept connections
Step 3: Test the container
╰─ docker exec -it 66b79bef5769 /bin/sh ─╯
/ #
/ #
/ # su postgres
/ $ psql
postgres-docker-build-db-1 | 2022-06-25 17:30:23.626 UTC [70] FATAL: role "postgres" does not exist
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist
/ $ psql -U postgres
postgres-docker-build-db-1 | 2022-06-25 17:30:53.226 UTC [73] FATAL: role "postgres" does not exist
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist
/ $ psql -U postgres -h localhost
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "postgres" does not exist
postgres-docker-build-db-1 | 2022-06-25 17:31:04.399 UTC [75] FATAL: role "postgres" does not exist
/ $ psql -U postgres -h localhost -p 5432
postgres-docker-build-db-1 | 2022-06-25 17:31:14.465 UTC [77] FATAL: role "postgres" does not exist
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "postgres" does not exist
When I run docker volume ls, I do not see anything:
╰─ docker volume ls ─╯
DRIVER VOLUME NAME

Postgres database connectivity issues when using docker-compose

I am trying to dockerize the REST api written in golang, but having troubles connecting to postgres database (the app works fine without docker). Below are my Dockerfile and docker-compose.yml
FROM golang:alpine
RUN mkdir /app
ADD . /app
WORKDIR /app
COPY go.mod .
RUN go mod download
COPY . .
RUN go build -o main .
CMD ["/app/main"]
docker-compose.yml
version: '3'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres
environment:
POSTGRES_DB: test_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1234
ports:
- 5432:5432
my-go-app:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
# Postgres Details
DB_HOST: postgres
DB_DRIVER: postgres
DB_USER: postgres
DB_PASSWORD: 1234
DB_NAME: test_db
DB_PORT: 5432
# APP details
APP_PROTOCOL: http
APP_HOST: localhost
APP_PORT: 10000
ALLOWED_ORIGINS: "*"
ports:
- 10000:10000
depends_on:
- postgres
Database properties: host = "localhost", port = 5432, user = "postgres", password="1234", dbname="test_db" (all specified in docker-compose.yml). App itself runs on port 10000
When running docker up and docker-compose up --build I'm getting sql: database is closed error
Starting rest_db_my-go-app_1 ... done
Attaching to rest_db_postgres_1, rest_db_my-go-app_1
my-go-app_1 | 2021/04/02 12:54:07 Connection Failed to Open
my-go-app_1 | 2021/04/02 12:54:07 Starting development server at http://127.0.0.1:10000/
my-go-app_1 | 2021/04/02 12:54:07 Quit the server with CONTROL-C.
my-go-app_1 |
my-go-app_1 | (/app/main.go:185)
my-go-app_1 | [2021-04-02 12:54:07] sql: database is closed
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-04-02 12:02:15.807 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2021-04-02 12:02:15.808 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-04-02 12:02:15.808 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-04-02 12:02:15.815 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-04-02 12:02:15.831 UTC [26] LOG: database system was shut down at 2021-04-02 12:02:14 UTC
postgres_1 | 2021-04-02 12:02:15.837 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2021-04-02 12:02:25.830 UTC [1] LOG: received fast shutdown request
postgres_1 | 2021-04-02 12:02:25.834 UTC [1] LOG: aborting any active transactions
postgres_1 | 2021-04-02 12:02:25.835 UTC [1] LOG: background worker "logical replication launcher" (PID 32) exited with exit code 1
postgres_1 | 2021-04-02 12:02:25.835 UTC [27] LOG: shutting down
postgres_1 | 2021-04-02 12:02:25.862 UTC [1] LOG: database system is shut down
How to solve this database connectivity issue? Thanks in advance

Can't connect Go Application to the postgres container in docker-compose

I have been trying to connect my go application to postgresql db using docker-compose.
Here is my Dockerfile for the web server
FROM golang:latest
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN go mod download
EXPOSE 5000
CMD ["go","run","main.go"]
And this is my docker-compose.yml file
version: '3'
services:
db:
image: postgres:latest
restart: always
network_mode: bridge
environment:
POSTGRES_USER: factly
POSTGRES_PASSWORD: qwertyui
POSTGRES_DB: factly
ports:
- "5432:5432"
server:
build: .
depends_on:
- db
ports:
- "5000:5000"
But I can't connect to the postgresql db as it gives a connection refused error message from the
Go serverStarting factly_db_1 ...
Starting factly_db_1 ... done
Recreating factly_server_1 ...
Recreating factly_server_1 ... done
Attaching to factly_db_1, factly_server_1
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2020-08-19 06:11:17.151 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-19 06:11:17.151 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-08-19 06:11:17.152 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-08-19 06:11:17.266 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-19 06:11:17.906 UTC [26] LOG: database system was shut down at 2020-08-19 05:56:39 UTC
db_1 | 2020-08-19 06:11:18.213 UTC [1] LOG: database system is ready to accept connections
server_1 | host=127.0.0.1 port=5432 user=factly password=qwertyui dbname=factly sslmode=disable
server_1 | 2020/08/19 06:11:21 dial tcp 127.0.0.1:5432: connect: connection refused
server_1 | exit status 1
factly_server_1 exited with code 1
I have tried running just the postgres container and connecting it with the Go Application which works perfectly fine but running the Go Application inside a container gives the same error(Which makes me feel the Go web server container couldn't connect outside of it's network).
How can I correct this?
I guess the problem is you are trying to connect to 127.0.0.1 from the go program instead use the hostname of the database container db.

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.

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