我必须在docker postgres上安装hstore

psql -d template1 -c 'create extension hstore';

如果我从我的容器中移除该行,它会工作,但我必须自己执行hstore个安装,我必须告诉我的项目中的每个人这样做,这不是一个好的做法

这是我的yml file美元

  postgres:
    build:
      context: .
      dockerfile: dockerfiles/devdb.dockerfile
    environment:
      POSTGRES_USER: uih
      POSTGRES_PASSWORD: uIhbod!
      POSTGRES_DB: uih_portal
    ports:
        - "5433:5432"

这是我的docker文件devdb.dockerfile

FROM postgres:9.5

RUN mkdir -p /var/lib/postgresql-static/data
ENV PGDATA /var/lib/postgresql-static/data

# psql -d template1 -c 'create extension hstore;'
CMD ["psql", "-d", "template1", "-c", "'create extension hstore;'"]

RUN echo "hstore extension installed"

构建之后,我无法运行它

$ docker-compose up postgres
Recreating uihportal_postgres_1
Attaching to uihportal_postgres_1
postgres_1       | psql: could not connect to server: No such file or directory
postgres_1       |      Is the server running locally and accepting
postgres_1       |      connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
uihportal_postgres_1 exited with code 2

Question:

如何从dockerfile安装hstore

推荐答案

它之所以失败,是因为在构建过程中,Postgres没有在容器中运行,只有在容器运行时,它才在CMD中启动.

entrypoint script for the Docker image支持运行任何设置步骤.sql或./docker-entrypoint-initdb.d目录中的sh文件将在容器启动时执行.

因此,您可以通过将扩展设置放在SQL脚本中,并将脚本复制到init目录中的映像中来实现这一点:

> cat hstore.sql
create extension hstore
> cat Dockerfile
FROM postgres:9.5
COPY hstore.sql /docker-entrypoint-initdb.d

构建该映像时,SQL脚本将在正确的位置执行,因此每当容器从映像运行时,它都会安装扩展.

Postgresql相关问答推荐

在Haskell中定义新类型与持久化类型的惯用方法

从数据集中提取彼此喜欢的用户(&Q;)

Psql:Windows 10上奇怪的错误输出编码

将XML解析从T-SQL迁移到Postgres时出现问题

docker-compose.yml用于使用postgres的spring-boot应用程序

Postgres 对 WHERE 子句并行使用两个索引

在 jOOQ 中使用 $$ 引用字符串

EF Core添加迁移目标postgreeSQL返回无法加载类型'Microsoft.EntityFrameworkCore.Storage.IRelationalValueBufferFactoryFactory

PostgreSql maintenance_work_mem 在索引创建期间增加

联合所有 postgresql Select 子句保留顺序

使用 postgresql Select 整数作为位和状态表

如何在 PostgreSQL hstore 中使用通配符查询值

带有初始数据的 docker postgres 不会在提交中持久化

SQL:当谈到 NOT IN 和 NOT EQUAL TO 时,哪个更有效,为什么?

在远程机器上Restore dump

没有查询要创建的in ... error

Postgresql:在插入列时(或之前)自动小写文本

Postgres 表中列的顺序会影响性能吗?

如何为 postgres 连接设置 application_name?

判断 PostgreSQL 中的角色是否设置了密码