1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

postgreSQL 18のdockerにはまった

1
Last updated at Posted at 2026-04-25

docker-compose で postgreSQLの18系を作ろうと思ったらハマったので

ダメな設定

services:
  # Postgresql
  postgres:
    image: postgres:18
    container_name: postgres
    ports:
      - 5432:5432
    restart: always
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - PGTZ=Asia/Tokyo
    volumes:
      - ./.docker/postgresql/init:/docker-entrypoint-initdb.d
      - ./.docker/postgresql/data:/var/lib/postgresql/data/

なんでか起動しない。。。

$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
999db974d0b4 postgres:18 "docker-entrypoint.s…" 15 minutes ago Restarting (1) 54 seconds ago postgres

ログをみてみる

$ sudo docker logs postgres

ログの内容

Error: in 18+, these Docker images are configured to store database data in a
       format which is compatible with "pg_ctlcluster" (specifically, using
       major-version-specific directory names).  This better reflects how
       PostgreSQL itself works, and how upgrades are to be performed.

       See also https://github.com/docker-library/postgres/pull/1259

       Counter to that, there appears to be PostgreSQL data in:
         /var/lib/postgresql/data (unused mount/volume)

       This is usually the result of upgrading the Docker image without
       upgrading the underlying database using "pg_upgrade" (which requires both
       versions).

       The suggested container configuration for 18+ is to place a single mount
       at /var/lib/postgresql which will then place PostgreSQL data in a
       subdirectory, allowing usage of "pg_upgrade --link" without mount point
       boundary issues.

       See https://github.com/docker-library/postgres/issues/37 for a (long)
       discussion around this process, and suggestions for how to do so.

/var/lib/postgresql/data (unused mount/volume) ってまさか、ディレクトリ構造かわったのか?

起動した設定

services:
  # Postgresql
  postgres:
    image: postgres:18
    container_name: postgres
    ports:
      - 5432:5432
    restart: always
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - PGTZ=Asia/Tokyo
    volumes:
      - ./.docker/postgresql/init:/docker-entrypoint-initdb.d
      - ./.docker/postgresql:/var/lib/postgresql/

.docker/postgresql/18/docker の下が、いままでの dataディレクトリですね。。。

total 136
drwx------ 19 dnsmasq root             4096 Apr 25 14:02 .
drwxr-xr-x  3 root    root             4096 Apr 25 14:02 ..
-rw-------  1 dnsmasq systemd-journal     3 Apr 25 14:02 PG_VERSION
drwx------  6 dnsmasq systemd-journal  4096 Apr 25 14:02 base
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:04 global
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_commit_ts
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_dynshmem
-rw-------  1 dnsmasq systemd-journal  5753 Apr 25 14:02 pg_hba.conf
-rw-------  1 dnsmasq systemd-journal  2681 Apr 25 14:02 pg_ident.conf
drwx------  4 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_logical
drwx------  4 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_multixact
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_notify
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_replslot
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_serial
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_snapshots
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_stat
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_stat_tmp
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_subtrans
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_tblspc
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_twophase
drwx------  4 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_wal
drwx------  2 dnsmasq systemd-journal  4096 Apr 25 14:02 pg_xact
-rw-------  1 dnsmasq systemd-journal    88 Apr 25 14:02 postgresql.auto.conf
-rw-------  1 dnsmasq systemd-journal 32557 Apr 25 14:02 postgresql.conf
-rw-------  1 dnsmasq systemd-journal    36 Apr 25 14:02 postmaster.opts
-rw-------  1 dnsmasq systemd-journal    99 Apr 25 14:02 postmaster.pid

補足
.env

POSTGRES_DB=postgresdb
POSTGRES_USER=postgres
POSTGRES_PASSWORD=xxxxxxxx

ファイアウォール(ubuntuの場合)

sudo ufw allow 5432/tcp

select * From version();

PostgreSQL 18.3 (Debian 18.3-1.pgdg13+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit

ってことで、参考になれば

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?