2
0

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のDockerコンテナのLocaleをCにする

Last updated at Posted at 2025-02-03

What's?

PostgreSQLのDockerコンテナを使う時に、意識していないとLocale(というかcollateとctype)がen_US.utf8になっていたのでCにしたいなということで。

ソートなどで気にすることになりますよね。

環境

$ docker version
Client: Docker Engine - Community
 Version:           27.5.1
 API version:       1.47
 Go version:        go1.22.11
 Git commit:        9f9e405
 Built:             Wed Jan 22 13:42:47 2025
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.5.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.11
  Git commit:       4c9b3b0
  Built:            Wed Jan 22 13:41:09 2025
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.25
  GitCommit:        bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
 runc:
  Version:          1.2.4
  GitCommit:        v1.2.4-0-g6c52b3f
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

なにも指定しない場合

特になにも指定せず、PostgreSQLのDockerコンテナを起動します。

$ docker container run -it --rm --name postgres \
  -e POSTGRES_PASSWORD=xxxxx \
  postgres:17.2-bookworm

この時の結果。

$ docker container exec -it postgres psql -U postgres -l
                                                    List of databases
   Name    |  Owner   | Encoding | Locale Provider |  Collate   |   Ctype    | Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+------------+------------+--------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           |
 template0 | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/postgres          +
           |          |          |                 |            |            |        |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/postgres          +
           |          |          |                 |            |            |        |           | postgres=CTc/postgres
(3 rows)

CollateCTypeen_US.utf8になっています。

これを変えたいというのが今回のお題です。

POSTGRES_INITDB_ARGS環境変数を指定する

この挙動を変えるにはinitdb時の指定を変更することになりますが、これを行うのがPOSTGRES_INITDB_ARGS環境変数です。

This optional environment variable can be used to send arguments to postgres initdb. The value is a space separated string of arguments as postgres initdb would expect them. This is useful for adding functionality like data page checksums: -e POSTGRES_INITDB_ARGS="--data-checksums".

DockerHub / postgres

Cにするには、次のどちらかでしょうね。

$ docker container run -it --rm --name postgres \
  -e POSTGRES_PASSWORD=xxxxx \
  -e POSTGRES_INITDB_ARGS=--no-locale \
  postgres:17.2-bookworm


$ docker container run -it --rm --name postgres \
  -e POSTGRES_PASSWORD=xxxxx \
  -e POSTGRES_INITDB_ARGS=--locale=C \
  postgres:17.2-bookworm

結果は、どちらも以下になります。

$ docker container exec -it postgres psql -U postgres -l
                                                 List of databases
   Name    |  Owner   | Encoding  | Locale Provider | Collate | Ctype | Locale | ICU Rules |   Access privileges
-----------+----------+-----------+-----------------+---------+-------+--------+-----------+-----------------------
 postgres  | postgres | SQL_ASCII | libc            | C       | C     |        |           |
 template0 | postgres | SQL_ASCII | libc            | C       | C     |        |           | =c/postgres          +
           |          |           |                 |         |       |        |           | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | libc            | C       | C     |        |           | =c/postgres          +
           |          |           |                 |         |       |        |           | postgres=CTc/postgres
(3 rows)

おや、EncodingSQL_ASCIIですね。

ではこうでしょうか。

$ docker container run -it --rm --name postgres \
  -e POSTGRES_PASSWORD=xxxxx \
  -e POSTGRES_INITDB_ARGS='--encoding=UTF-8 --no-locale' \
  postgres:17.2-bookworm


$ docker container run -it --rm --name postgres \
  -e POSTGRES_PASSWORD=xxxxx \
  -e POSTGRES_INITDB_ARGS='--encoding=UTF-8 --locale=C' \
  postgres:17.2-bookworm

今度はこうなりました。

$ docker container exec -it postgres psql -U postgres -l
                                                List of databases
   Name    |  Owner   | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+---------+-------+--------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | C       | C     |        |           |
 template0 | postgres | UTF8     | libc            | C       | C     |        |           | =c/postgres          +
           |          |          |                 |         |       |        |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | C       | C     |        |           | =c/postgres          +
           |          |          |                 |         |       |        |           | postgres=CTc/postgres
(3 rows)

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?