Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

669
Views
How to run sql script for Postgres in Docker?

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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!

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!