使用架构,表创建Postgres容器

时间:2020-07-31 07:45:54

标签: postgresql docker

我正在尝试启动一个postgres数据库容器。启动容器后,我还希望在其中创建模式和表。

我正在尝试将.sql文件复制到docker入口点中。

我的Dockerfile:

FROM postgres 
ENV POSTGRES_PASSWORD postgres 
ENV POSTGRES_DB MY_APP_DB
COPY my-app-db-ddl.sql /docker-entrypoint-initdb.d/

DDL文件内容:

CREATE SCHEMA MY_APP_DB;

CREATE TABLE MY_APP_DB.ACCESS_MASTER
(
    "ACCESS_ID" character varying COLLATE pg_catalog."default" NOT NULL,
    "ROLE_ID" character varying COLLATE pg_catalog."default",
);

上面的脚本有助于创建数据库,但不能创建架构/表。我该如何实现?

请帮助。

我的容器日志:

2020-08-05 08:49:25.428 UTC [47] LOG:  database system was shut down at 2020-08-05 08:49:25 UTC
2020-08-05 08:49:25.433 UTC [46] LOG:  database system is ready to accept connections
 done
server started
CREATE DATABASE


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/imcsysdb.sql
CREATE SCHEMA
CREATE TABLE


2020-08-05 08:49:25.654 UTC [46] LOG:  received fast shutdown request
waiting for server to shut down....2020-08-05 08:49:25.656 UTC [46] LOG:  aborting any active transactions
2020-08-05 08:49:25.657 UTC [46] LOG:  background worker "logical replication launcher" (PID 53) exited with exit code 1
2020-08-05 08:49:25.658 UTC [48] LOG:  shutting down
2020-08-05 08:49:25.675 UTC [46] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2020-08-05 08:49:25.768 UTC [1] LOG:  starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-08-05 08:49:25.769 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-08-05 08:49:25.769 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2020-08-05 08:49:25.772 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-08-05 08:49:25.789 UTC [73] LOG:  database system was shut down at 2020-08-05 08:49:25 UTC
2020-08-05 08:49:25.794 UTC [1] LOG:  database system is ready to accept connections

1 个答案:

答案 0 :(得分:0)

0.Dockerfile

from postgres
  1. 更正sql中的错误:
CREATE SCHEMA MY_APP_DB;

CREATE TABLE MY_APP_DB.ACCESS_MASTER
(
    "ACCESS_ID" character varying COLLATE pg_catalog."default" NOT NULL,
    "ROLE_ID" character varying COLLATE pg_catalog."default"
);
  1. 在您本地的目录中创建一个目录,并以docker -v /opt/import1:/docker-entrypoint-initdb.d/的身份挂载

cat /opt/import/setup.sql

root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# cat /opt/import1/setup.sql 
create schema my_app_db;
CREATE TABLE my_app_db.access_master ("ACCESS_ID" character varying COLLATE pg_catalog."default" NOT NULL,"ROLE_ID" character varying COLLATE pg_catalog."default");

root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# cat Dockerfile 
from postgres

root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
sathyadev/postgres   latest              683dd2ba4015        About an hour ago   313MB
postgres             latest              4b52913b0a3a        8 days ago          313MB

docker run --name 683dd2ba4015 -p 5432:5432 -v /opt/import1:/docker-entrypoint-initdb.d/ -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres

root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
53a6f7972b91        postgres            "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:5432->5432/tcp   683dd2ba4015

  1. 验证
postgres=# set schema 'my_app_db';
SET
postgres=# select * from access_master;
 ACCESS_ID | ROLE_ID 
-----------+---------
(0 rows)

postgres=# 

完整日志供您参考

root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# cat Dockerfile 
from postgres

root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker build --tag sathyadev/postgres:0.1 .
Sending build context to Docker daemon  4.096kB
Step 1/1 : from postgres
 ---> 4b52913b0a3a
Successfully built 4b52913b0a3a
Successfully tagged sathyadev/postgres:0.1
root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
postgres             latest              4b52913b0a3a        11 days ago         313MB
sathyadev/postgres   0.1                 4b52913b0a3a        11 days ago         313MB
root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker run --name 4b52913b0a3a -p 5432:5432 -v /opt/import1:/docker-entrypoint-initdb.d/ -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres
d4709527d3f42393cf5a857ebd650d582da918ef964075b418a63f5acef17516
root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d4709527d3f4        postgres            "docker-entrypoint.s…"   14 seconds ago      Up 13 seconds       0.0.0.0:5432->5432/tcp   4b52913b0a3a
root@ubuntu:/home/sathya/Desktop/stackoverflo/docker/postgres# docker exec -ti d4709527d3f4 bash
root@d4709527d3f4:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@d4709527d3f4:/# su postgres
postgres@d4709527d3f4:/$ psql
psql (12.3 (Debian 12.3-1.pgdg100+1))
Type "help" for help.

postgres=# set schema 'my_app_db';
SET
postgres=# select * from access_master
postgres-# ;
 ACCESS_ID | ROLE_ID 
-----------+---------
(0 rows)