Docker Container: Relation X doesn't exist in database - postgresql

I am developping a java application, I created an image of the database PostgreSQL by using docker.
I have a folder "docker-entrypoint-initdb.d" which contains files to create tables, data.
When I launch my application, I get this exception:
org.postgresql.util.PSQLException: ERROR: relation
"myerp.sequence_comptable" does not exist.
Here the content of docker-compose.yml:
version: '2'
services:
myerp.db:
image: postgres:10
ports:
- "127.0.0.1:9032:5432"
volumes:
- "./init/db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d"
environment:
- POSTGRES_DB=db_myerp
- POSTGRES_USER=usr_myerp
- POSTGRES_PASSWORD=myerp
When I launch command docker compose up:
Creating network "dev_default" with the default driver
Creating dev_myerp.db_1 ... done
Attaching to dev_myerp.db_1
myerp.db_1 | The files belonging to this database system will be owned by user "postgres".
myerp.db_1 | This user must also own the server process.
myerp.db_1 |
myerp.db_1 | The database cluster will be initialized with locale "en_US.utf8".
myerp.db_1 | The default database encoding has accordingly been set to "UTF8".
myerp.db_1 | The default text search configuration will be set to "English".
myerp.db_1 |
myerp.db_1 | Data page checksums are disabled.
myerp.db_1 |
myerp.db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
myerp.db_1 | creating subdirectories ... ok
myerp.db_1 | selecting default max_connections ... 100
myerp.db_1 | selecting default shared_buffers ... 128MB
myerp.db_1 | selecting dynamic shared memory implementation ... posix
myerp.db_1 | creating configuration files ... ok
myerp.db_1 | running bootstrap script ... ok
myerp.db_1 | performing post-bootstrap initialization ... ok
myerp.db_1 | syncing data to disk ... ok
myerp.db_1 |
myerp.db_1 | Success. You can now start the database server using:
myerp.db_1 |
myerp.db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
myerp.db_1 |
myerp.db_1 |
myerp.db_1 | WARNING: enabling "trust" authentication for local connections
myerp.db_1 | You can change this by editing pg_hba.conf or using the option -A, or
myerp.db_1 | --auth-local and --auth-host, the next time you run initdb.
myerp.db_1 | waiting for server to start....2018-09-24 14:53:52.027 UTC [40] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
myerp.db_1 | 2018-09-24 14:53:52.061 UTC [41] LOG: database system was shut down at 2018-09-24 14:53:51 UTC
myerp.db_1 | 2018-09-24 14:53:52.069 UTC [40] LOG: database system is ready to accept connections
myerp.db_1 | done
myerp.db_1 | server started
myerp.db_1 | CREATE DATABASE
myerp.db_1 |
myerp.db_1 |
myerp.db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
myerp.db_1 |
myerp.db_1 | waiting for server to shut down....2018-09-24 14:53:52.541 UTC [40] LOG: received fast shutdown request
myerp.db_1 | 2018-09-24 14:53:52.552 UTC [40] LOG: aborting any active transactions
myerp.db_1 | 2018-09-24 14:53:52.555 UTC [40] LOG: worker process: logical replication launcher (PID 47) exited with exit code 1
myerp.db_1 | 2018-09-24 14:53:52.559 UTC [42] LOG: shutting down
myerp.db_1 | 2018-09-24 14:53:52.588 UTC [40] LOG: database system is shut down
myerp.db_1 | done
myerp.db_1 | server stopped
myerp.db_1 |
myerp.db_1 | PostgreSQL init process complete; ready for start up.
myerp.db_1 |
myerp.db_1 | 2018-09-24 14:53:52.657 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
myerp.db_1 | 2018-09-24 14:53:52.657 UTC [1] LOG: listening on IPv6 address "::", port 5432
myerp.db_1 | 2018-09-24 14:53:52.667 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
myerp.db_1 | 2018-09-24 14:53:52.696 UTC [58] LOG: database system was shut down at 2018-09-24 14:53:52 UTC
myerp.db_1 | 2018-09-24 14:53:52.705 UTC [1] LOG: database system is ready to accept connections
myerp.db_1 | 2018-09-24 14:57:16.551 UTC [68] ERROR: relation "myerp.sequence_ecriture_comptable" does not exist at character 46
myerp.db_1 | 2018-09-24 14:57:16.551 UTC [68] STATEMENT:
myerp.db_1 | SELECT derniere_valeur FROM myerp.sequence_ecriture_comptable
myerp.db_1 | WHERE annee = $1 AND journal_code = $2
Can you help me please?
Thank you

I was getting the same error:
psql:/docker-entrypoint-initdb.d/init2.sql:20: ERROR: relation "tablename" does not exist
It can be solved by adding the following line at the very beginning of every
docker-entrypoint-initdb.d/* sql file:
\c dbname;

Related

docker-entrypoint-initdb.d/* files ignored on docker-compose first run

I try to setup a Postgresql db in my docker-compose and it worked as expected for a moment. But now, my migration sql scripts aren't runned anymore, even after a fresh new build (sudo docker-compose build or sudo docker-compose up --build) and after removing the persistent volume created locally (sudo rm -rf db-data).
$ sudo docker-compose up
[+] Running 14/14
⠿ db Pulled 10.7s
⠿ a2abf6c4d29d Pull complete 2.6s
⠿ e1769f49f910 Pull complete 2.8s
⠿ 33a59cfee47c Pull complete 2.9s
⠿ 461b2090c345 Pull complete 3.1s
⠿ 8ed8ab6290ac Pull complete 3.4s
⠿ 495e42c822a0 Pull complete 3.5s
⠿ 18e858c71c58 Pull complete 3.6s
⠿ 594792c80d5f Pull complete 3.7s
⠿ 794976979956 Pull complete 8.2s
⠿ eb5e1a73c3ca Pull complete 8.3s
⠿ 6d6360292cba Pull complete 8.3s
⠿ 131e916e1a28 Pull complete 8.4s
⠿ 757a73507e2e Pull complete 8.5s
[+] Running 2/2
⠿ Container db-1 Created 0.2s
⠿ Container app-1 Created 0.1s
Attaching to app-1, db-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 dynamic shared memory implementation ... posix
db-1 | selecting default max_connections ... 100
db-1 | selecting default shared_buffers ... 128MB
db-1 | selecting default time zone ... Etc/UTC
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 |
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 | initdb: 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....2022-01-26 14:57:29.498 UTC [49] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db-1 | 2022-01-26 14:57:29.506 UTC [49] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2022-01-26 14:57:29.529 UTC [50] LOG: database system was shut down at 2022-01-26 14:57:29 UTC
db-1 | 2022-01-26 14:57:29.542 UTC [49] LOG: database system is ready to accept connections
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/00000000000000_diesel_initial_setup
db-1 |
db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/2021-10-21-140200_initial_schemes
db-1 |
db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/2022-01-24-120647_fill_tables
db-1 |
db-1 | waiting for server to shut down...2022-01-26 14:57:29.858 UTC [49] LOG: received fast shutdown request
db-1 | .2022-01-26 14:57:29.865 UTC [49] LOG: aborting any active transactions
db-1 | 2022-01-26 14:57:29.868 UTC [49] LOG: background worker "logical replication launcher" (PID 56) exited with exit code 1
db-1 | 2022-01-26 14:57:29.868 UTC [51] LOG: shutting down
db-1 | 2022-01-26 14:57:29.914 UTC [49] 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 | 2022-01-26 14:57:29.993 UTC [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db-1 | 2022-01-26 14:57:29.994 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db-1 | 2022-01-26 14:57:29.994 UTC [1] LOG: listening on IPv6 address "::", port 5432
db-1 | 2022-01-26 14:57:30.008 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2022-01-26 14:57:30.026 UTC [63] LOG: database system was shut down at 2022-01-26 14:57:29 UTC
db-1 | 2022-01-26 14:57:30.038 UTC [1] LOG: database system is ready to accept connections
As you can see, my docker-entrypoint-initdb.d/* files are all being ignored. Notice that my 3 migrations scripts run fine on a local pg instance.
My docker-compose looks lik this:
version: '3.7'
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=***
- POSTGRES_PASSWORD=***
- POSTGRES_DB=***
ports:
- '5433:5432'
volumes:
- ./db-data:/var/lib/postgresql/data
- ./migrations:/docker-entrypoint-initdb.d/ # copy the sql migrations
[...]
volumes:
db-data:
According to poatgres image entrypoint only below file type executed:
*.sh
*.sql
*.sql.gz
*.sql.xz

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

App couldn't connect to Postgresql Database with docker-compose

I'm trying to get my web application running using docker-compose.
My Dockerfile
FROM golang:1.15
WORKDIR /goprojects/vnlist
COPY . .
RUN go get github.com/revel/revel
RUN go get github.com/revel/cmd/revel
RUN go get -d -v ./...
EXPOSE 9000
CMD [ "revel", "run", "-a", "vnlist" ]
My docker-compose.yml
version: '3.3'
services:
web:
build: .
ports:
- "9000:9000"
db:
image: "postgres"
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgresql123"
POSTGRES_DB: "vnlist_db"
volumes:
- db-data:/foo
volumes:
db-data:
The DBsetup file
package models
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var DB *gorm.DB
var (
DB_HOST = "localhost"
DB_PORT = "5432"
DB_USER = "postgres"
DB_PASSWORD = "postgresql123"
DB_NAME = "vnlist_db"
DB_TIMEZONE = "Asia/Jakarta"
)
func InitDB() {
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println("DB Error: ", err)
}
db.AutoMigrate(&VN{}, &User{})
DB = db
}
And then, I ran docker-compose up --build. Here's the result of the command:
Successfully built 1239cf1fceea
Successfully tagged vnlist_web:latest
Creating vnlist_db_1 ... done
Creating vnlist_web_1 ... done
Attaching to vnlist_db_1, vnlist_web_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 default timezone ... Etc/UTC
db_1 | selecting dynamic shared memory implementation ... posix
db_1 | creating configuration files ... ok
web_1 | Revel executing: run a Revel application
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....2021-01-18 14:25:35.070 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-01-18 14:25:35.437 UTC [48] LOG: database system was shut down at 2021-01-18 14:25:30 UTC
db_1 | 2021-01-18 14:25:35.530 UTC [47] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
web_1 | WARN 14:25:35 harness.go:175: No http.addr specified in the app.conf listening on localhost interface only. This will not allow external access
to your application
web_1 | Changed detected, recompiling
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 | waiting for server to shut down....2021-01-18 14:25:39.271 UTC [47] LOG: received fast shutdown request
db_1 | 2021-01-18 14:25:39.420 UTC [47] LOG: aborting any active transactions
db_1 | 2021-01-18 14:25:39.424 UTC [47] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
db_1 | 2021-01-18 14:25:39.424 UTC [49] LOG: shutting down
db_1 | .2021-01-18 14:25:41.059 UTC [47] 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 | 2021-01-18 14:25:41.178 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2021-01-18 14:25:41.178 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2021-01-18 14:25:41.445 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-01-18 14:25:41.906 UTC [75] LOG: database system was shut down at 2021-01-18 14:25:40 UTC
db_1 | 2021-01-18 14:25:42.031 UTC [1] LOG: database system is ready to accept connections
web_1 | Parsing packages, (may require download if not cached)... Completed
web_1 | INFO 14:27:12 app run.go:35: Running revel server
web_1 | INFO 14:27:12 app plugin.go:9: Go to /#tests to run the tests.
web_1 |
web_1 | 2021/01/18 14:27:12 /goprojects/vnlist/app/models/DbSetup.go:24
web_1 | [error] failed to initialize database, got error failed to connect to `host=localhost user=postgres database=vnlist_db`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)
web_1 | DB Error: failed to connect to `host=localhost user=postgres database=vnlist_db`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)
web_1 |
web_1 | 2021/01/18 14:27:12 /root/go/pkg/mod/gorm.io/driver/postgres#v1.0.6/migrator.go:157 failed to connect to `host=localhost user=postgres database=vnlist_db`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)
web_1 | [0.659ms] [rows:0] CREATE TABLE "vns" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"title" text,"genre" text,"original_language" text,"translated" boolean,PRIMARY KEY ("id"))
web_1 |
web_1 | 2021/01/18 14:27:12 /root/go/pkg/mod/gorm.io/driver/postgres#v1.0.6/migrator.go:130 failed to connect to `host=localhost user=postgres database=vnlist_db`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)
web_1 | [1.093ms] [rows:0] CREATE INDEX "idx_vns_deleted_at" ON "vns" ("deleted_at")
web_1 | Revel engine is listening on.. localhost:35495
web_1 |
web_1 | Time to recompile 1m36.9527518s
web_1 | Revel proxy is listening, point your browser to : 9000
As we can see on the last few lines, the database failed to connect.
I've tried to change the DB's credential but the problem persists.
My question is, how to fix this?
Your DB_HOST would be the name of the database service DB_HOST = "db" instead of "localhost"

How to run a *.sh file on Postgres Docker-Compose startup

I am trying to create a new DB + User on Postgres when starting a new Docker-Compose Postgres image. For some reason, the init-user-db.sh file is not being copied inside docker-entrypoint-initdb.d folder on the container.
Any idea why?
Thanks
docker-compose.yml:
version: '3'
services:
db:
image: postgres:11
volumes:
- ./db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
ports:
- '5432:5432'
init-user-db.sh:
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" <<-EOSQL
CREATE USER bh_ideas_user;
CREATE DATABASE bh_ideas ENCODING UTF8;
GRANT ALL PRIVILEGES ON DATABASE bh_ideas TO bh_ideas_user;
ALTER USER bh_ideas_user WITH PASSWORD 'password123';
ALTER USER bh_ideas_user WITH SUPERUSER;
EOSQL
This is the output I receive:
$ docker-compose up && exit 0
Creating ideas-api_db_1 ... done
Attaching to ideas-api_db_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
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 | ****************************************************
db_1 | WARNING: No password has been set for the database.
db_1 | This will allow anyone with access to the
db_1 | Postgres port to access your database. In
db_1 | Docker's default configuration, this is
db_1 | effectively any other container on the same
db_1 | system.
db_1 |
db_1 | Use "-e POSTGRES_PASSWORD=password" to set
db_1 | it in "docker run".
db_1 | ****************************************************
db_1 | waiting for server to start....2019-01-19 10:28:21.673 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2019-01-19 10:28:21.692 UTC [49] LOG: database system was shut down at 2019-01-19 10:28:21 UTC
db_1 | 2019-01-19 10:28:21.699 UTC [48] 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 | 2019-01-19 10:28:21.763 UTC [48] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2019-01-19 10:28:21.766 UTC [48] LOG: aborting any active transactions
db_1 | 2019-01-19 10:28:21.767 UTC [48] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1
db_1 | 2019-01-19 10:28:21.769 UTC [50] LOG: shutting down
db_1 | 2019-01-19 10:28:21.794 UTC [48] 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-01-19 10:28:21.903 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2019-01-19 10:28:21.903 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2019-01-19 10:28:21.910 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2019-01-19 10:28:21.930 UTC [57] LOG: database system was shut down at 2019-01-19 10:28:21 UTC
db_1 | 2019-01-19 10:28:21.937 UTC [1] LOG: database system is ready to accept connections
From the docker inspect, the binding is properly done:
"Binds": ["/host_mnt/d/Projects/ideas-api/db/initdb.d:/docker-entrypoint-initdb.d:rw"],

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()