I am following this post to run SQL script to create default table.
Here is my docker compose file
version: "3"
services:
web:
build: .
volumes:
- ./:/usr/src/app/
- /usr/src/app/node_modules
ports:
- “3000:3000”
depends_on:
- postgres
postgres:
container_name: postgres
build:
context: .
dockerfile: pgDockerfile
environment:
POSTGRES_PASSWORD: test
POSTGRES_USER: test
ports:
- 5432:5432
Here is my pgDockerfile
FROM postgres:9.6-alpine
# copy init sql
ADD 00-initial.sql /docker-entrypoint-initdb.d/
Here is my sql script
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS test (
id text NOT NULL,
title varchar(200) NOT NULL
);
I can build and run docker-compose up, and I see the following message:
postgres | CREATE DATABASE
postgres |
postgres | CREATE ROLE
postgres |
postgres |
postgres | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/00-initial-data.sql
postgres | CREATE EXTENSION
postgres | CREATE TABLE
postgres |
postgres |
postgres | LOG: received fast shutdown request
postgres | LOG: aborting any active transactions
postgres | waiting for server to shut down....LOG: autovacuum launcher shutting down
postgres | LOG: shutting down
postgres | LOG: database system is shut down
postgres | done
postgres | server stopped
postgres |
postgres | PostgreSQL init process complete; ready for start up.
postgres |
postgres | LOG: database system was shut down at 2017-03-22 21:36:16 UTC
postgres | LOG: MultiXact member wraparound protections are now enabled
postgres | LOG: database system is ready to accept connections
postgres | LOG: autovacuum launcher started
It seems like the db is shut down after the table is created. I think that’s the standard process for Postgres Docker but when I login to Postgres, I don’t see my table that is supposed to be there.
I login through
docker exec -it $(my postgres container id) sh
#su - postgres
#psql
# \d => No relations found.
I am not sure if this is the right way to create default data for Postgres.
I think that your docker works as intended, but it's not quite doing what you think. When setting POSTGRES_USER, but not POSTGRES_DB, the system will default to using the user name as database name, so your table is in the test database!
Using \l to list your databases will show the databases, and then you can use \c test to connect to the database. Once connected, \d will list your relations!