Docker Container: Relation X doesn't exist in database

1.5k Views Asked by At

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

1

There are 1 best solutions below

1
On

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;