简体   繁体   English

如何在Docker中为Postgres运行sql脚本?

[英]How to run sql script for Postgres in Docker?

I am following this post to run SQL script to create default table. 我正在关注此帖子以运行SQL脚本来创建默认表。

Here is my docker compose file 这是我的docker撰写文件

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 这是我的pgDockerfile

FROM postgres:9.6-alpine

# copy init sql
ADD 00-initial.sql /docker-entrypoint-initdb.d/

Here is my sql script 这是我的sql脚本

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: 我可以构建并运行docker-compose up ,我看到以下消息:

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. 在创建表之后,似乎关闭了db。 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. 我认为这是Postgres Docker的标准流程,但是当我登录Postgres时,我看不到我的桌子应该在那里。

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. 我不确定这是否是为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! 设置POSTGRES_USER而不是POSTGRES_DB ,系统将默认使用用户名作为数据库名称,因此您的表位于test数据库中!

Using \\l to list your databases will show the databases, and then you can use \\c test to connect to the database. 使用\\l列出数据库将显示数据库,然后您可以使用\\c test连接到数据库。 Once connected, \\d will list your relations! 连接后, \\d将列出您的关系!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM