简体   繁体   English

初始化postgres数据库(不使用/docker-entrypoint-initdb.d)

[英]Initialize postgres database (without using /docker-entrypoint-initdb.d)

I'm trying to initialize a database without using the entry point directory . 我试图在不使用入口点目录的情况下初始化数据库。

Here is a minimal Dockerfile: 这是最小的Dockerfile:

FROM postgres:latest

POSTGRES_DB db
POSTGRES_USER user
POSTGRES_PASSWORD password

ADD db.sql /directory/
ADD script.sh /directory/

CMD ["sh", "/directory/script.sh"] 
# Or ENTRYPOINT ["/directory/script.sh"]?

And script.sh : script.sh

psql -d db -U user < /directory/db.sql

This does not work because postgres isn't up when the script is run. 这不起作用,因为运行脚本时postgres没有启动。

How can I run db.sql without using /docker-entrypoint-initdb.d ? 如何在不使用/docker-entrypoint-initdb.d情况下运行db.sql

If you look at the standard postgres image's entrypoint script the mechanism to support the /docker-entrypoint-initdb.d directory is pretty intricate: it needs to bootstrap the database directory and initial user and database, then it starts the database server in the background and runs everything in that directory, and finally runs the database for real. 如果查看标准的postgres映像的入口点脚本 ,支持/docker-entrypoint-initdb.d目录的机制非常复杂:它需要引导数据库目录以及初始用户和数据库,然后它将在后台启动数据库服务器并运行该目录中的所有内容,最后运行数据库。 If you're trying to replicate this setup, you have to do all of these steps yourself. 如果要复制此设置,则必须自己完成所有这些步骤。

There are other ways to set up a database, though. 但是,还有其他方法可以建立数据库。 You can create an empty database and then run your application's migrations normally to create the initial schema. 您可以创建一个空数据库,然后正常运行应用程序的迁移以创建初始架构。 If you have an SQL file that normally you'd run against a database running the psql client tool, you can do the exact same thing with Docker 如果您有一个通常在运行psql客户端工具的数据库上运行的SQL文件,则可以使用Docker执行完全相同的操作

docker run -d -p 5432:5432 --name postgres postgres:12
psql -h localhost < db.sql

暂无
暂无

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

相关问题 使用 /docker-entrypoint-initdb.d 初始化 postgres docker 时不会创建表 - Tables are not created when initialize postgres docker with /docker-entrypoint-initdb.d 使用 docker-entrypoint-initdb.d 脚本初始化 PostgreSQL 容器 - Initialize PostgreSQL Container with docker-entrypoint-initdb.d script 在以前工作的配置中忽略 Postgres 数据库的 /docker-entrypoint-initdb.d 错误 - ignoring /docker-entrypoint-initdb.d error for Postgres database in a configuration that previously worked Docker-Compose + Postgres:/docker-entrypoint-initdb.d/init.sql:权限被拒绝 - Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied docker alpine postgres在撰写中不执行docker-entrypoint-initdb.d脚本 - docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts docker-compose postgres 在 docker-entrypoint-initdb.d 中运行脚本后重新启动 - docker-compose postgres restart after running scripts in docker-entrypoint-initdb.d Docker postgres 不在 docker-entrypoint-initdb.d 中运行 init 文件 - Docker postgres does not run init file in docker-entrypoint-initdb.d 为什么 postgres 容器在 Gitlab CI 中忽略 /docker-entrypoint-initdb.d/* - Why is postgres container ignoring /docker-entrypoint-initdb.d/* in Gitlab CI flyway 无法连接到 docker-entrypoint-initdb.d 脚本中的 postgres 容器 - flyway unable to connect to postgres container within docker-entrypoint-initdb.d script docker-entrypoint-initdb.d 错误解释器:权限被拒绝 - docker-entrypoint-initdb.d bad interpreter: Permission denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM