在Docker Compose中运行psql命令

huangapple go评论55阅读模式
英文:

run psql commands in docker compose

问题

以下是我试图运行的内容:

createuser postgres

createdb DB

createdb TEST_DB

psql -d DB -c "GRANT CREATE ON DATABASE 'DB' TO postgres"

psql -d DB -c "CREATE EXTENSION IF NOT EXISTS 'uuid-ossp'"

psql -d DB -c "CREATE EXTENSION IF NOT EXISTS 'pg_trgm'"

psql -d TEST_DB -c "GRANT CREATE ON DATABASE 'TEST_DB' TO postgres"

psql -d TEST_DB -c "CREATE EXTENSION IF NOT EXISTS 'uuid-ossp'"

psql -d TEST_DB -c "CREATE EXTENSION IF NOT EXISTS 'pg_trgm'"
英文:

I have a docker compose file, which spins up postgres db. How can I run some psql commands after this container is started?

在Docker Compose中运行psql命令

This is what I'm trying to run:

createuser postgres

createdb DB

createdb TEST_DB

psql -d DB -c "GRANT CREATE ON DATABASE "DB" TO postgres"

psql -d DB -c "CREATE EXTENSION IF NOT EXISTS "uuid-ossp""

psql -d DB -c "CREATE EXTENSION IF NOT EXISTS "pg_trgm""

psql -d TEST_DB -c "GRANT CREATE ON DATABASE "TEST_DB" TO postgres"

psql -d TEST_DB -c "CREATE EXTENSION IF NOT EXISTS "uuid-ossp""

psql -d TEST_DB -c "CREATE EXTENSION IF NOT EXISTS "pg_trgm""

答案1

得分: 0

官方 postgres 镜像支持初始化脚本,您可以将命令保存在 /docker-entrypoint-initdb.d 中的 .sql 文件中,它们将被执行。

> 如果您想在从此镜像派生的镜像中执行额外的初始化操作,
> 在 /docker-entrypoint-initdb.d 下添加一个或多个 .sql、.sql.gz 或 *.sh 脚本(如有必要,请创建该目录)。
> 在 entrypoint 调用 initdb 来创建默认的 postgres 用户和数据库之后,它将运行任何 *.sql 文件,运行任何可执行的 *.sh
> 脚本,并在启动服务之前源化该目录中找到的任何非可执行的 *.sh 脚本。

英文:

The official postgres image supports initialization scripts, you can save your commands in .sql file in /docker-entrypoint-initdb.d and they will be executed

> If you would like to do additional initialization in an image derived
> from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under
> /docker-entrypoint-initdb.d (creating the directory if necessary).
> After the entrypoint calls initdb to create the default postgres user
> and database, it will run any *.sql files, run any executable *.sh
> scripts, and source any non-executable *.sh scripts found in that
> directory to do further initialization before starting the service.

答案2

得分: 0

postgres镜像初始化脚本已完成!
我在Dockerfile中使用COPY命令将一些.sh文件复制到容器中。

英文:

postgres image initialization scripts did it!
I used COPY in my Dockerfile to copy some .sh files to the container.

huangapple
  • 本文由 发表于 2023年6月26日 02:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定