簡體   English   中英

在 postgres dockerfile 上運行 psql

[英]run psql on postgres dockerfile

我從 postgres 標准圖像安裝 postgis,然后我應該運行 psql 命令來創建擴展 POSTGIS。 我認為我的問題是在 psql 命令中設置正確的 -h 參數,但我不確定。 任何的想法?

 FROM postgres:12.1 MAINTAINER xxx #ARG A_DB_USER='postgres' #ARG A_DB_PASS='postgres' ARG A_DB_NAME='postgres' ARG A_TZ='Europe/Zurich' #ENV DB_USER=${A_DB_USER} #ENV DB_PASS=${A_DB_PASS} ENV DB_NAME=${A_DB_NAME} ENV TZ=${A_TZ} # Adjusting Timezone in the system RUN echo $TZ > /etc/timezone && \\ apt-get update && apt-get install -y tzdata && \\ rm /etc/localtime && \\ ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \\ dpkg-reconfigure -f noninteractive tzdata && \\ apt-get clean # install postgis RUN apt-get update && \\ apt-get install -y postgis && \\ apt-get clean USER postgres #Add password "postgres" to user postgres, create db, add .sql RUN /etc/init.d/postgresql start && psql -U postgres -d mydb -h localhost -c "CREATE EXTENSION postgis;" EXPOSE 5432

錯誤是: psql: error: could not connect to server: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? ERROR: Service 'istsos-db' failed to build: The command '/bin/sh -c /etc/init.d/postgresql start && psql -U postgres -d istsos -h localhost -c "CREATE EXTENSION postgis;"' returned a non-zero code: 2 psql: error: could not connect to server: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? ERROR: Service 'istsos-db' failed to build: The command '/bin/sh -c /etc/init.d/postgresql start && psql -U postgres -d istsos -h localhost -c "CREATE EXTENSION postgis;"' returned a non-zero code: 2

編輯:我用 docker-compose 運行它

 version: '3.7' services: app-db: build: context: ./ dockerfile: Dockerfile-pg image: app-pg:1.0.0 restart: always environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: app POSTGRES_DB: app-db volumes: - ./docker-entrypoint-initdb.d/init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh - v-app-pgdata:/var/lib/postgresql - v-app-pglog:/data/var/log/postgresql - v-app-pgconf:/etc/postgresql app-main: build: context: ./ dockerfile: Dockerfile-tar-cp image: app-main:1.0.0 restart: always ports: - 80:80 volumes: v-app-pgdata: name: v-app-pgdata v-app-pglog: name: v-app-pglog v-app-pgconf: name: v-app-pgconf

我嘗試使用POSTGRES_DB: app-db並指定psql -h app-db但 docker-build 返回: No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning). psql: error: could not connect to server: could not translate host name "app-db" to address: Name or service not known ERROR: Service 'app-db' failed to build: The command '/bin/sh -c /etc/init.d/postgresql start && psql -U postgres -d app -h app-db -c "CREATE EXTENSION postgis;"' returned a non-zero code: 2 No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning). psql: error: could not connect to server: could not translate host name "app-db" to address: Name or service not known ERROR: Service 'app-db' failed to build: The command '/bin/sh -c /etc/init.d/postgresql start && psql -U postgres -d app -h app-db -c "CREATE EXTENSION postgis;"' returned a non-zero code: 2

我也嘗試使用 localhost 沒有成功。 有任何想法嗎?

為您的運行腳本嘗試以下代碼。

RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE EXTENSION postgis;"

編輯:

請添加以下內容以確保端口已打開並且 pg 也可以偵聽外部 IP。

RUN echo "host all  all    0.0.0.0/0  md5" >> /folder/to/conf/pg_hba.conf

添加收聽所有ip

RUN echo "listen_addresses='*'" >> /folder/to/conf/postgresql.conf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM