gpt4 book ai didi

postgresql - Postgres 不在 docker 中初始化数据库

转载 作者:行者123 更新时间:2023-11-29 13:44:29 26 4
gpt4 key购买 nike

我正在尝试创建一个新的 postgres 数据库。

仅供初学者使用 - 创建新卷:

docker volume create test

然后开始图像。

docker run  -v test:/var/lib/postgresql/data -p 5432:5432 nec-postgres

现在这是我的 Dockerfile:

FROM postgres:10.4

ENV POSTGRES_USER=user
ENV POSTGRES_PASSWORD=pass
ENV POSTGRES_DB=smdr

COPY *.sql /docker-entrypoint-initdb.d/

看起来很简单,根据https://hub.docker.com/_/postgres/假设创建一个数据库,如果卷为空,则在该数据库上运行初始化脚本。

当我第一次开始我的图像时,我得到了预期的结果:

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/1_database.sql
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE

在我看到的初始化之后(同样,正如预期的那样)

2018-06-05 06:55:51.855 UTC [37] LOG:  received fast shutdown request
waiting for server to shut down....
2018-06-05 06:55:51.859 UTC [37] LOG: aborting any active transactions
2018-06-05 06:55:51.863 UTC [37] LOG: worker process: logical replication launcher (PID 44) exited with exit code 1
2018-06-05 06:55:51.864 UTC [39] LOG: shutting down
2018-06-05 06:55:51.904 UTC [37] LOG: database system is shut down
done
server stopped

PostgreSQL init process complete; ready for start up.

2018-06-05 06:55:51.971 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2018-06-05 06:55:51.972 UTC [1] LOG: listening on IPv6 address "::", port 5432
2018-06-05 06:55:51.978 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-05 06:55:51.993 UTC [109] LOG: database system was shut down at 2018-06-05 06:55:51 UTC
2018-06-05 06:55:52.000 UTC [1] LOG: database system is ready to accept connections

现在,无论我做什么,我的数据库都是空的。连接到数据库后,我在数据库中看不到任何表或任何数据。

我试过 10.4 和 10.4-alpine,我试过 9.6-alpine,但似乎无法使它工作。

如果有任何建议,我将不胜感激。


更新#1:

这是 postgres 的完整启动日志:

root@smdr:/home/smdr/nec# docker logs smdr_nec-postgres.ytyrdw0r2j76aytclj3d9tusj.0vd91n153gxeydsbjp730tlvj
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....2018-06-05 16:00:25.543 UTC [37] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-05 16:00:25.560 UTC [38] LOG: database system was shut down at 2018-06-05 16:00:25 UTC
2018-06-05 16:00:25.566 UTC [37] LOG: database system is ready to accept connections
done
server started
CREATE DATABASE

CREATE ROLE


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/1_database.sql
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/2_indexes.sql
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/3_init.sql
INSERT 0 1
INSERT 0 1
INSERT 0 1


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/4_functions.sql
CREATE FUNCTION


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/5_roles.sql


2018-06-05 16:00:26.551 UTC [37] LOG: received fast shutdown request
waiting for server to shut down....2018-06-05 16:00:26.555 UTC [37] LOG: aborting any active transactions
2018-06-05 16:00:26.558 UTC [37] LOG: worker process: logical replication launcher (PID 44) exited with exit code 1
2018-06-05 16:00:26.559 UTC [39] LOG: shutting down
2018-06-05 16:00:26.612 UTC [37] LOG: database system is shut down
done
server stopped

PostgreSQL init process complete; ready for start up.

2018-06-05 16:00:26.671 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2018-06-05 16:00:26.672 UTC [1] LOG: listening on IPv6 address "::", port 5432
2018-06-05 16:00:26.679 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-05 16:00:26.697 UTC [109] LOG: database system was shut down at 2018-06-05 16:00:26 UTC
2018-06-05 16:00:26.703 UTC [1] LOG: database system is ready to accept connections

最佳答案

初始化种子数据库和 postgre docker 容器

version: '2'
services:
postgresql:
image: postgres:9.6.5
# uncomment to maintan persistance
# volumes:
# uncomment to maintan persistance
# - ~/volumes/jhipster/postgree/postgresql/:/var/lib/postgresql/data/
# init with the initial sql file
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_USER=postgree
- POSTGRES_PASSWORD=
ports:
- 5432:5432

注意#1

请注意,您可以将多个脚本链接到该文件夹​​,它们都会运行(按字母顺序)

volumes:
- ./init1.sql:/docker-entrypoint-initdb.d/1-schema.sql
- ./init2.sql:/docker-entrypoint-initdb.d/2-data.sql

注意 #2

确保您指向目录而不是文件。因此,当您有大量文件时,您可以挂载目录,而不是直接引用文件

volumes:
- ./path/to/my/host/folder:/docker-entrypoint-initdb.d/
- ./path/to/my/host/folder:/docker-entrypoint-initdb.d/

关于postgresql - Postgres 不在 docker 中初始化数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com