如何在docker中为postgres运行sql脚本

我正在关注这个post来运行SQL脚本来创build默认表。

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

这是我的pgDockerfile

 FROM postgres:9.6-alpine # copy init sql ADD 00-initial.sql /docker-entrypoint-initdb.d/ 

这是我的SQL脚本

 CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TABLE IF NOT EXISTS test (    id text NOT NULL,    title varchar(200) NOT NULL ); 

我可以构build并运行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 

数据库似乎在表创build后closures。 我认为这是postgres docker的标准过程,但是当我login到postgres时,我没有看到我的表应该在那里。

我通过login

 docker exec -it $(my postgres container id) sh #su - postgres #psql # \d => No relations found. 

我不确定这是否是为postgres创build默认数据的正确方法。 任何人都可以帮助我吗? 非常感谢!