skip to Main Content

I am trying to set up a postgres container to start and run initializing the creation of a table. I’ve succeeded with the straight image from docker but now that I am trying to extend the image a little to create tables when it’s produced and I can’t get it running. Based off what I’ve read here How to create User/Database in script for Docker Postgres, this is what I have:

Dockerfile:

FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/

init.sql:

CREATE TABLE incident_disposition (
incident_disposition_code VARCHAR,
incident_disposition_code_description VARCHAR
);

From what I understand, FROM library . . . pulls the postgres image from docker hub and the COPY pushes my init.sql script into the entry point so there is no need for a big dockerfile correct?

I then build the image no issue:

Build

 docker build -t my_postgres_image .

But when I run I get the issues:

Run

docker run --name testing my_postgres_image --publish 8000:8080 --detach  -e POSTGRES_PASSWORD=postgres -d postgres

Errors from logs

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html

Attempt from comment:

docker container logs testing
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok


Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2020-03-26 14:06:51.064 UTC [46] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-03-26 14:06:51.072 UTC [46] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-03-26 14:06:51.108 UTC [47] LOG:  database system was shut down at 2020-03-26 14:06:50 UTC
2020-03-26 14:06:51.119 UTC [46] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
CREATE TABLE


2020-03-26 14:06:51.231 UTC [46] LOG:  received fast shutdown request
waiting for server to shut down....2020-03-26 14:06:51.232 UTC [46] LOG:  aborting any active transactions
2020-03-26 14:06:51.233 UTC [46] LOG:  background worker "logical replication launcher" (PID 53) exited with exit code 1
2020-03-26 14:06:51.234 UTC [48] LOG:  shutting down
2020-03-26 14:06:51.290 UTC [46] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2020-03-26 14:06:51.345 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-03-26 14:06:51.345 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-03-26 14:06:51.345 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2020-03-26 14:06:51.361 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-03-26 14:06:51.387 UTC [64] LOG:  database system was shut down at 2020-03-26 14:06:51 UTC
2020-03-26 14:06:51.398 UTC [1] LOG:  database system is ready to accept connections
2020-03-26 14:07:27.715 UTC [72] ERROR:  relation "incident_disposition" does not exist at character 15
2020-03-26 14:07:27.715 UTC [72] STATEMENT:  select * from incident_disposition;

2

Answers


  1. In addition to comments

    Due to recent docker image’s updates postgres images do not allow to connect to DB without a password from anywhere. So you need to specify username/password

    docker run -p 8000:8080 -e POSTGRES_PASSWORD=postgres –name testing -d my_postgres_image

    Or if you still don’t want to use password, you can just set POSTGRES_HOST_AUTH_METHOD=trust environment variable:

    docker run -p 8000:8080 -e POSTGRES_HOST_AUTH_METHOD=trust –name testing -d my_postgres_image

    Login or Signup to reply.
  2. It is a typical Initialization scripts issue.
    You can file the full explaination in postgresql docker page. https://hub.docker.com/_/postgres

    Here is the brief intro:
    1. One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts.
    note:
    in your case, you may need clean the historical docker containers(stopped) by
    step 1: docker ps |grep
    step 2: docker rm -f -v

    Or if you are using docker-compose, the historical orchestrator could be easily removed by docker-compose down -v.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search