Docker Compose + TypeORM: No Connection - postgresql

I know there are plenty of questions around this topic, but I can't make it work for my (fairly straightforward setup). What am I missing? I guess we can neglect pgadmin out of the equation, but I kept in there for full disclosure.
docker-compose.yml
version: '3.8'
services:
postgres:
image: postgres:14.2-alpine
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
POSTGRES_DB: mydatabase
PGDATA: /data/mydatabase
volumes:
- pgstore:/var/lib/postgresql/data
ports:
- '5432:5432'
networks:
- postgres
restart: unless-stopped
pgadmin:
image: dpage/pgadmin4:6.5
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin#admin.com}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- '5050:80'
networks:
- postgres
depends_on:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
pgstore:
pgadmin:
Starting it with docker-compose up yields:
Creating network "my-app_postgres" with driver "bridge"
Creating my-app_postgres_1 ... done
Creating my-app_pgadmin_1 ... done
Attaching to my-app_postgres_1, my-app_pgadmin_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 /data/mydatabase ... 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
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... sh: locale: not found
postgres_1 | 2022-09-06 14:03:48.965 UTC [32] WARNING: no usable system locales were found
postgres_1 | ok
postgres_1 | syncing data to disk ... 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 | ok
postgres_1 |
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /data/mydatabase -l logfile start
postgres_1 |
postgres_1 | waiting for server to start....2022-09-06 14:03:49.890 UTC [38] LOG: starting PostgreSQL 14.2 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres_1 | 2022-09-06 14:03:49.891 UTC [38] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-09-06 14:03:49.894 UTC [39] LOG: database system was shut down at 2022-09-06 14:03:49 UTC
postgres_1 | 2022-09-06 14:03:49.896 UTC [38] 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: ignoring /docker-entrypoint-initdb.d/*
postgres_1 |
postgres_1 | waiting for server to shut down....2022-09-06 14:03:50.195 UTC [38] LOG: received fast shutdown request
postgres_1 | 2022-09-06 14:03:50.196 UTC [38] LOG: aborting any active transactions
postgres_1 | 2022-09-06 14:03:50.197 UTC [38] LOG: background worker "logical replication launcher" (PID 45) exited with exit code 1
postgres_1 | 2022-09-06 14:03:50.197 UTC [40] LOG: shutting down
postgres_1 | 2022-09-06 14:03:50.204 UTC [38] 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 | 2022-09-06 14:03:50.307 UTC [1] LOG: starting PostgreSQL 14.2 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres_1 | 2022-09-06 14:03:50.308 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2022-09-06 14:03:50.308 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2022-09-06 14:03:50.310 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-09-06 14:03:50.313 UTC [52] LOG: database system was shut down at 2022-09-06 14:03:50 UTC
postgres_1 | 2022-09-06 14:03:50.315 UTC [1] LOG: database system is ready to accept connections
pgadmin_1 | NOTE: Configuring authentication for SERVER mode.
pgadmin_1 |
pgadmin_1 | [2022-09-06 14:03:56 +0000] [1] [INFO] Starting gunicorn 20.1.0
pgadmin_1 | [2022-09-06 14:03:56 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
pgadmin_1 | [2022-09-06 14:03:56 +0000] [1] [INFO] Using worker: gthread
pgadmin_1 | [2022-09-06 14:03:56 +0000] [89] [INFO] Booting worker with pid: 89
In my application, I initialize TypeORM with the following options:
const options = {
default: {
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: process.env.DATABASE_PORT,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
logging: false,
},
};
Whereas my .env holds the following information:
DATABASE_TYPE=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=mydatabase
I can access the container on the command line with:
docker exec -it my-app psql -U postgres mydatabase
and list the \l the databases and see mydatabase. But I get Did not find any relations. for \d because the application does not connect to the database and therefore there are no relations yet.
More information:
the application is running outside of the container
the application is not throwing an error, it just tries to connect indefinitely
I am using Docker container on MacOS
What am I missing here? Any help appreciated!

Related

Problem with postgres for 2 concurrent docker deployments on the same host

I'm having issues with postgres for 2 docker deployments on the same host. I need to deploy the same app with docker compose on the same host. I have one docker-compose file and two .env for each instance. The app is built with Ruby and Hanami web framework.
When deploying the first instance (production) it runs fine. With the second instance (staging) I get this error during docker-compose setup:
PostgreSQL Database directory appears to contain a database; Skipping initialization
.
.
.
2022-07-30 16:13:48.978 UTC [33] FATAL: password authentication failed for user "some_user"
postgres_1 | 2022-07-30 16:13:48.978 UTC [33] DETAIL: Role "some_user" does not exist.
I also see the port being used is 5432, which is the one used for production.
For staging, I've specified the other_user and a different port as you can see in the file below.
Here are the files in question with which I wanted to separate these DB instances:
docker-compose
version: '3'
services:
postgres:
image: postgres
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
ports:
- ${POSTGRES_PORT}:${POSTGRES_PORT}
volumes:
- postgres:/var/lib/postgresql/data
web:
build: .
command: >
bash -c "bundle exec hanami db migrate
&& bundle exec rake initial_settings:add_default_language
&& bundle exec rake initial_settings:add_session_validity
&& bundle exec rake import_user:create
&& bundle exec rake super_admin:create
&& bundle exec hanami assets precompile
&& bundle exec hanami server
&& cp -r apps/wordrocket/assets/webfonts public/webfonts
&& cp -r apps/wordrocket/assets/webfonts public/assets/webfonts
&& cp -r apps/wordrocket/assets/images/sort*.png public/assets
&& cp -r apps/wordrocket/assets/images/sort*.png public
&& cp -r apps/wordrocket/assets/images/ui-icons*.png public/assets/wordrocket
&& mkdir public/assets/images
&& cp -r apps/wordrocket/assets/images/sort*.png public/assets/images"
volumes:
- ./hanami_log/hanami_app.log:/usr/src/app/hanami_log/hanami_app.log
links:
- postgres
depends_on:
- postgres
nginx:
image: nginx:alpine
restart: unless-stopped
tty: true
ports:
- "${NGINX_PORT}:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./nginx_log:/var/log/nginx
depends_on:
- web
volumes:
postgres:
web:
nginx:
Production .env
POSTGRES_USER=some_user
POSTGRES_PASSWORD=some_password
POSTGRES_DB=app_production
NGINX_PORT=7080
COMPOSE_PROJECT_NAME=myAPP
POSTGRES_PORT=5432
Staging .env.staging
POSTGRES_USER=other_user
POSTGRES_PASSWORD=other_password
POSTGRES_DB=app_staging
NGINX_PORT=7081
COMPOSE_PROJECT_NAME=myAPP_staging
POSTGRES_PORT=5433
Makefile
production:
docker-compose -f docker-compose.yml --env-file .env up
staging:
docker-compose -f docker-compose.yml --env-file .env.staging up
I'm guessing I don't separate the postgres instances/dbs correctly, but I'm not sure. Also, is this a Docker or host problem?
+++ EDIT +++
I've made a separate docker-compose file for staging in order to differentiate between volumes.
production is the same:
volumes:
- postgres:/var/lib/postgresql/data
.
.
.
volumes:
postgres:
web:
nginx:
staging:
volumes:
- postgres_staging:/var/lib/postgresql/data
.
.
.
volumes:
postgres_staging:
web:
nginx:
This is the startup log up to the first error:
nginx_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
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 ... Etc/UTC
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... ok
nginx_1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
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....2022-07-31 10:03:12.510 UTC [47] LOG: starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2022-07-31 10:03:12.513 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-07-31 10:03:12.524 UTC [48] LOG: database system was shut down at 2022-07-31 10:03:12 UTC
postgres_1 | 2022-07-31 10:03:12.529 UTC [47] 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: ignoring /docker-entrypoint-initdb.d/*
postgres_1 |
postgres_1 | waiting for server to shut down...2022-07-31 10:03:12.745 UTC [47] LOG: received fast shutdown request
postgres_1 | .2022-07-31 10:03:12.748 UTC [47] LOG: aborting any active transactions
postgres_1 | 2022-07-31 10:03:12.749 UTC [47] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
postgres_1 | 2022-07-31 10:03:12.749 UTC [49] LOG: shutting down
postgres_1 | 2022-07-31 10:03:12.771 UTC [47] 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 | 2022-07-31 10:03:12.865 UTC [1] LOG: starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2022-07-31 10:03:12.865 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2022-07-31 10:03:12.865 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2022-07-31 10:03:12.871 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-07-31 10:03:12.879 UTC [61] LOG: database system was shut down at 2022-07-31 10:03:12 UTC
postgres_1 | 2022-07-31 10:03:12.885 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2022-07-31 10:03:13.014 UTC [68] FATAL: password authentication failed for user "some_user"
Now this is the only instance being started. The production is not running even and it still tries to authenticate the production user.
Is it ignoring the .env.staging file?
+++ END EDIT +++
Best, Seba

Hasura installation problem using docker - PostgreSQL not responding

I want to install Hasura using docker. According to the Hasura doc, I created the docker-compose.yml file. then I compose docker with docker-compose up. In my terminal log, a message comes LOG: database system is ready to accept connections. But when I go to the http://localhost:8080/, I find nothing.
I don't understand where is the problem and how to solve it?
docker-compose.yml file
version: '3.6'
services:
postgres:
image: postgres:12
restart: always
volumes:
- ./db_data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgrespassword
graphql-engine:
image: hasura/graphql-engine:v2.0.10
ports:
- "8080:8080"
depends_on:
- "postgres"
restart: always
environment:
## postgres database to store Hasura metadata
HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword#postgres:5432/postgres
## this env var can be used to add the above postgres database to Hasura as a data source. this can be removed/updated based on your needs
PG_DATABASE_URL: postgres://postgres:postgrespassword#postgres:5432/postgres
## enable the console served by server
HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
## enable debugging mode. It is recommended to disable this in production
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
## uncomment next line to set an admin secret
# HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
volumes:
db_data:
my terminal:
sumon#error-inside hasura % docker-compose up
hasura_postgres_1 is up-to-date
9675a01ed8fd_hasura_graphql-engine_1 is up-to-date
Attaching to hasura_postgres_1, 9675a01ed8fd_hasura_graphql-engine_1
9675a01ed8fd_hasura_graphql-engine_1 | qemu: uncaught target signal 11 (Segmentation fault) - core dumped
9675a01ed8fd_hasura_graphql-engine_1 | qemu: uncaught target signal 11 (Segmentation fault) - core dumped
9675a01ed8fd_hasura_graphql-engine_1 | qemu: uncaught target signal 11 (Segmentation fault) - core dumped
9675a01ed8fd_hasura_graphql-engine_1 | qemu: uncaught target signal 11 (Segmentation fault) - core dumped
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 ... Etc/UTC
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... ok
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....2021-10-23 04:46:45.991 UTC [50] LOG: starting PostgreSQL 12.8 (Debian 12.8-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2021-10-23 04:46:45.993 UTC [50] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-10-23 04:46:46.023 UTC [51] LOG: database system was shut down at 2021-10-23 04:46:44 UTC
postgres_1 | 2021-10-23 04:46:46.039 UTC [50] 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...2021-10-23 04:46:46.242 UTC [50] LOG: received fast shutdown request
postgres_1 | .2021-10-23 04:46:46.243 UTC [50] LOG: aborting any active transactions
postgres_1 | 2021-10-23 04:46:46.245 UTC [50] LOG: background worker "logical replication launcher" (PID 57) exited with exit code 1
postgres_1 | 2021-10-23 04:46:46.245 UTC [52] LOG: shutting down
postgres_1 | 2021-10-23 04:46:46.279 UTC [50] 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 | 2021-10-23 04:46:46.358 UTC [1] LOG: starting PostgreSQL 12.8 (Debian 12.8-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2021-10-23 04:46:46.359 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-10-23 04:46:46.359 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-10-23 04:46:46.363 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-10-23 04:46:46.395 UTC [69] LOG: database system was shut down at 2021-10-23 04:46:46 UTC
postgres_1 | 2021-10-23 04:46:46.413 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2021-10-23 04:51:52.553 UTC [1] LOG: received fast shutdown request
postgres_1 | 2021-10-23 04:51:52.555 UTC [1] LOG: aborting any active transactions
postgres_1 | 2021-10-23 04:51:52.556 UTC [1] LOG: background worker "logical replication launcher" (PID 75) exited with exit code 1
postgres_1 | 2021-10-23 04:51:52.559 UTC [70] LOG: shutting down
postgres_1 | 2021-10-23 04:51:52.596 UTC [1] LOG: database system is shut down
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-10-23 04:52:02.912 UTC [1] LOG: starting PostgreSQL 12.8 (Debian 12.8-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2021-10-23 04:52:02.913 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-10-23 04:52:02.913 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-10-23 04:52:02.917 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-10-23 04:52:02.949 UTC [28] LOG: database system was shut down at 2021-10-23 04:51:52 UTC
postgres_1 | 2021-10-23 04:52:02.967 UTC [1] LOG: database system is ready to accept connections
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-10-23 05:09:34.960 UTC [1] LOG: starting PostgreSQL 12.8 (Debian 12.8-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2021-10-23 05:09:34.960 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-10-23 05:09:34.960 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-10-23 05:09:34.964 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-10-23 05:09:34.996 UTC [28] LOG: database system was interrupted; last known up at 2021-10-23 04:57:03 UTC
postgres_1 | 2021-10-23 05:09:36.329 UTC [28] LOG: database system was not properly shut down; automatic recovery in progress
postgres_1 | 2021-10-23 05:09:36.342 UTC [28] LOG: redo starts at 0/164DF58
postgres_1 | 2021-10-23 05:09:36.342 UTC [28] LOG: invalid record length at 0/164E058: wanted 24, got 0
postgres_1 | 2021-10-23 05:09:36.342 UTC [28] LOG: redo done at 0/164E020
postgres_1 | 2021-10-23 05:09:36.367 UTC [1] LOG: database system is ready to accept connections
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-10-23 05:14:07.556 UTC [1] LOG: starting PostgreSQL 12.8 (Debian 12.8-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2021-10-23 05:14:07.556 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-10-23 05:14:07.556 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-10-23 05:14:07.560 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-10-23 05:14:07.591 UTC [28] LOG: database system was interrupted; last known up at 2021-10-23 05:09:36 UTC
postgres_1 | 2021-10-23 05:14:08.941 UTC [28] LOG: database system was not properly shut down; automatic recovery in progress
postgres_1 | 2021-10-23 05:14:08.944 UTC [28] LOG: redo starts at 0/164E0D0
postgres_1 | 2021-10-23 05:14:08.944 UTC [28] LOG: invalid record length at 0/164E108: wanted 24, got 0
postgres_1 | 2021-10-23 05:14:08.944 UTC [28] LOG: redo done at 0/164E0D0
postgres_1 | 2021-10-23 05:14:08.963 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2021-10-23 06:21:46.578 UTC [32] WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted

When starting Postgres docker image SQL from initdb.d is executed but db/schema/table are not created

I'm starting postgresql 12.3 image via dokcer-compose
I extended the image to execute simple SQL (create schema, table) on start.
From the logs I can see that the SQL commands are executed but when I log to the DB the changes are missing.
My Dockerfile:
FROM postgres
ENV INITDB_DIR /docker-entrypoint-initdb.d
ENV POSTGRES_PASSWORD=...
ENV POSTGRES_DB mydb
COPY initdb.d ${INITDB_DIR}
My docker-compose.yml:
version: "3.7"
services:
postgres:
image: postgres
build: ./postgres
restart: on-failure
ports:
- "5432:5432"
networks:
- postgres
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:...}
- POSTGRES_DB=mydb
networks:
postgres:
driver: bridge
create_schema.sql (located in ./postgres/initdb.d)
create schema "foo";
create table "foo"."message"(
"id" BIGINT NOT NULL,
"value" TEXT
);
alter table "foo"."message" add constraint "foo_message_pk" primary key("id");
And finally the logs produced when executing: docker-compose up --build postgres
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 ... Etc/UTC
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... ok
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-05-27 07:34:00.098 UTC [47] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2020-05-27 07:34:00.112 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-05-27 07:34:00.143 UTC [48] LOG: database system was shut down at 2020-05-27 07:33:59 UTC
postgres_1 | 2020-05-27 07:34:00.151 UTC [47] 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/create_schema.sql
postgres_1 | CREATE SCHEMA
postgres_1 | CREATE TABLE
postgres_1 | ALTER TABLE
postgres_1 |
postgres_1 |
postgres_1 | waiting for server to shut down....2020-05-27 07:34:00.588 UTC [47] LOG: received fast shutdown request
postgres_1 | 2020-05-27 07:34:00.593 UTC [47] LOG: aborting any active transactions
postgres_1 | 2020-05-27 07:34:00.595 UTC [47] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
postgres_1 | 2020-05-27 07:34:00.595 UTC [49] LOG: shutting down
postgres_1 | 2020-05-27 07:34:00.643 UTC [47] 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-05-27 07:34:00.708 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2020-05-27 07:34:00.709 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-05-27 07:34:00.709 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-05-27 07:34:00.719 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-05-27 07:34:00.744 UTC [74] LOG: database system was shut down at 2020-05-27 07:34:00 UTC
postgres_1 | 2020-05-27 07:34:00.753 UTC [1] LOG: database system is ready to accept connections
When I connect to the database only the default database (postgres) is present. i also tried the above without creating mydb database but still the schema or table were not present.

Docker Compose with PostgreSQL and Prometheus PostgreSQL Exporter refusing to connect?

I'm trying to solve this question regarding the PostgreSQL exporter to Prometheus (https://github.com/wrouesnel/postgres_exporter), Prometheus PostgreSQL server exporter example not working on MacOS?, running postgres and postgres_exporter containers connected to a user-defined bridge network rather than the host network, which seems not to work on Docker Desktop for Mac. I've created the following docker-compose.yml:
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: mypassword
networks:
- mynetwork
exporter:
image: wrouesnel/postgres_exporter
environment:
DATA_SOURCE_NAME: "postgresql://postgres:mypassword#db:5432/postgres?sslmode=disable"
ports:
- "9187:9187"
networks:
- mynetwork
networks:
mynetwork:
I am having two issues with this, however. Firstly, the logs from the exporter service show an error with this message:
"Error opening connection to database (postgresql://postgres:PASSWORD_REMOVED#db:5432/postgres?sslmode=disable): dial tcp 172.18.0.2:5432: connect: connection refused"
Here is the full output:
> docker-compose up
Creating network "postgres-performance-testing_mynetwork" with the default driver
Creating postgres-performance-testing_db_1 ... done
Creating postgres-performance-testing_exporter_1 ... done
Attaching to postgres-performance-testing_db_1, postgres-performance-testing_exporter_1
exporter_1 | time="2019-10-03T20:16:56Z" level=info msg="Established new database connection to \"db:5432\"." source="postgres_exporter.go:778"
exporter_1 | time="2019-10-03T20:16:56Z" level=error msg="Error opening connection to database (postgresql://postgres:PASSWORD_REMOVED#db:5432/postgres?sslmode=disable): dial tcp 172.18.0.2:5432: connect: connection refused" source="postgres_exporter.go:1348"
exporter_1 | time="2019-10-03T20:16:56Z" level=info msg="Starting Server: :9187" source="postgres_exporter.go:1459"
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
db_1 | selecting default timezone ... Etc/UTC
db_1 | selecting dynamic shared memory implementation ... posix
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... ok
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 |
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 | waiting for server to start....2019-10-03 20:16:57.084 UTC [42] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2019-10-03 20:16:57.095 UTC [43] LOG: database system was shut down at 2019-10-03 20:16:56 UTC
db_1 | 2019-10-03 20:16:57.101 UTC [42] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | waiting for server to shut down...2019-10-03 20:16:57.180 UTC [42] LOG: received fast shutdown request
db_1 | .2019-10-03 20:16:57.182 UTC [42] LOG: aborting any active transactions
db_1 | 2019-10-03 20:16:57.186 UTC [42] LOG: background worker "logical replication launcher" (PID 49) exited with exit code 1
db_1 | 2019-10-03 20:16:57.186 UTC [44] LOG: shutting down
db_1 | 2019-10-03 20:16:57.197 UTC [42] LOG: database system is shut down
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2019-10-03 20:16:57.297 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2019-10-03 20:16:57.297 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2019-10-03 20:16:57.299 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2019-10-03 20:16:57.310 UTC [51] LOG: database system was shut down at 2019-10-03 20:16:57 UTC
db_1 | 2019-10-03 20:16:57.314 UTC [1] LOG: database system is ready to accept connections
Is this because of the Prometheus exporter trying to connect to the database before it is ready to accept connections? (This would seem to be the case from the sequence of the logging).
Another problem is that if I browse to localhost:9187, the result is that I download a file containing HTML, rather than actually seeing a link to /metrics that I can follow:
Any idea how I can resolve these issues?
Without knowing how your images are built, I see two likely culprits that need to be addressed:
It seems that the exporter container is starting up first, and attempting to connect to the db container before postgres is even up and running. You may need to add depends_on: db in the exporter section.
You may need to edit pg_hba.conf on the db container before exporter can connect to it. By default, only local connections are allowed, so you'll have to append exporter's IP address (or use a /0 netmask, if this is a development-only environment)
If you fix these two, things should start working for you.
Disclosure: I am an EnterpriseDB (EDB) employee
postgres-exporter:
image: wrouesnel/postgres_exporter:v0.8.0
restart: always
environment:
#- DATA_SOURCE_NAME=postgresql://postgres:password#postgres-db:5432/postgres?sslmode=disable
- DATA_SOURCE_URI=postgres-db:5432/postgres?sslmode=disable
- DATA_SOURCE_USER=postgres
- DATA_SOURCE_PASS=password
ports:
- "9187:9187"
networks:
- postgres-prometheus
depends_on:
- postgres-db
Note in this example that the value for DATA_SOURCE_NAME has no double-quotes and that the value for DATA_SOURCE_URI (in case it's your preference) has no "postgresql://". It starts directly from the hostname.

Troubles with connect to database in Docker Compose

In one folder I have 4 files: base.py, Dockerfile, docker-compose.yml and wait-for-it.sh.
base.py:
import psycopg2
print("JJJJJJJJ")
conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'")
cur = conn.cursor()
cur.execute("CREATE TABLE test(id serial PRIMARY KEY, num int);")
Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get -y install python-pip
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install psycopg2-binary
COPY base.py base.py
COPY wait-for-it.sh wait-for-it.sh
RUN chmod +x /wait-for-it.sh
CMD ["python","base.py"]
docker-compose.yml
version: '3'
services:
db:
image: 'postgres:latest'
expose:
- "5432"
ports:
- "5559:5432"
environment:
POSTGRES_PASSWORD: pw1234
POSTGRES_DB: base123
aprrka:
build: .
depends_on:
- db
command: ["./wait-for-it.sh", "db:5432", "--", "python", "base.py"]
wait-fot-it.sh:
link to code in github
After docker-compose up I have this OUTPUT:
...
Creating postgres_db_1 ... done
Creating postgres_aprrka_1 ... done
Attaching to postgres_db_1, postgres_aprrka_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
db_1 | selecting dynamic shared memory implementation ... posix
aprrka_1 | wait-for-it.sh: waiting 15 seconds for db:5432
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... ok
db_1 |
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 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 | waiting for server to start....2018-08-10 09:59:13.529 UTC [38] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-08-10 09:59:13.582 UTC [39] LOG: database system was shut down at 2018-08-10 09:59:12 UTC
db_1 | 2018-08-10 09:59:13.599 UTC [38] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
db_1 | CREATE DATABASE
db_1 |
db_1 | ALTER ROLE
db_1 |
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | 2018-08-10 09:59:14.585 UTC [38] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2018-08-10 09:59:14.593 UTC [38] LOG: aborting any active transactions
db_1 | 2018-08-10 09:59:14.613 UTC [38] LOG: worker process: logical replication launcher (PID 45) exited with exit code 1
db_1 | 2018-08-10 09:59:14.613 UTC [40] LOG: shutting down
db_1 | 2018-08-10 09:59:14.660 UTC [38] LOG: database system is shut down
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2018-08-10 09:59:14.726 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2018-08-10 09:59:14.726 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2018-08-10 09:59:14.729 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-08-10 09:59:14.806 UTC [65] LOG: database system was shut down at 2018-08-10 09:59:14 UTC
db_1 | 2018-08-10 09:59:14.829 UTC [1] LOG: database system is ready to accept connections
db_1 | 2018-08-10 09:59:15.217 UTC [72] LOG: incomplete startup packet
aprrka_1 | wait-for-it.sh: db:5432 is available after 5 seconds
aprrka_1 | JJJJJJJJ
postgres_aprrka_1 exited with code 0
When I type command: psql -U postgres -h localhost -p 5559 in another terminal I can connect to base1234, but when type `\dt' i have no any relations, so my py file don't create table in postgres database. I think that no connection exist with postgres db. Please, help me
You need to close the cursor and commit all pending transactions to the database for the changes to be effective.
Add the following 2 lines at the end of your base.py:
cur.close()
conn.commit()