首页 > 解决方案 > Docker 文件 postgres 初始模式未执行

问题描述

我将 docker 文件更新到版本 3,但现在没有创建初始模式。我已经尝试过使用不同的卷运行它:docker-compose -f docker-compose.yml up

version: '3'
services:
  db-service:
    image: postgres:11.2
    volumes:
      - ./dbscripts/:/docker-entrypoint-initdb.d/
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=sample
    ports:
      - 5432:5432

标签: postgresqldockerdocker-compose

解决方案


检查以下两件事:


初始化脚本仅在第一次部署时触发。随后docker-compose ups,您可能还会在日志中看到以下消息:

db_1  | PostgreSQL Database directory appears to contain a database; Skipping initialization

要强制重新初始化,您可以擦除数据卷(这将删除条目,而不仅仅是架构)。
要找到路径,只需docker inspect <postgres-container-id>寻找HostConfig.Binds


脚本一直执行到第一个type "longvarbinary" does not exist错误。您将需要解决此问题。

db-service_1  | CREATE TABLE
db-service_1  | 2020-07-26 19:29:34.121 UTC [68] ERROR:  type "longvarbinary" does not exist at character 68

我在 postgres 容器中打开了一个控制台,您的sample数据库就在那里,还有一个在脚本中发生 .sql 错误之前创建的表:

docker exec -ti 22b0bb87561f sh
# psql -U postgres
psql (11.2 (Debian 11.2-1.pgdg90+1))
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 sample    | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgres=# \c sample
You are now connected to database "sample" as user "postgres".
sample=# \dt
                List of relations
 Schema |         Name         | Type  |  Owner   
--------+----------------------+-------+----------
 public | oauth_client_details | table | postgres
(1 row)


推荐阅读