Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

673
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda