イメージ取得とコンテナ作成
PostgreSQLのイメージを取得。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | >docker pull postgres Using default tag: latest latest: Pulling from library/postgres 1f7ce2fa46ab: Pull complete f2a11de1a8b0: Pull complete 5e6f9a8cd0bf: Pull complete 7419e1190e2e: Pull complete 8f39b869c008: Pull complete b194e21d762e: Pull complete ea4833c75f4e: Pull complete 587dfa1ea7d7: Pull complete 5358f267a946: Pull complete 68825fc4f7a3: Pull complete 761743441961: Pull complete c34ae0fdc6ad: Pull complete a3e3aea10a5c: Pull complete Digest: sha256:6dfee32131933ab4ca25a00360c3f427fdc134de56f9a90c6c9a4956b48aea85 Status: Downloaded newer image for postgres:latest docker.io/library/postgres:latest What's Next?   View summary of image vulnerabilities and recommendations → docker scout quickview postgres | 
イメージの確認。
| 1 2 3 4 | >docker images REPOSITORY                 TAG       IMAGE ID       CREATED        SIZE web                        latest    20e6fc2e9e4c   3 hours ago    287MB postgres                   latest    a20f35f462a4   3 days ago     425MB | 
コンテナ作成と実行。ここで、ホスト側で既にPostgreSQLが動いているので、ホスト側のポートを5431に設定している。
| 1 2 | >docker run -itd --name postgres -e POSTGRES_PASSWORD=postgres -p 5431:5432 postgres 68f874da73780157d3568c08ec2baf5faded1ea393ee314ba056b21e2e6e9e82 | 
動作確認
ターミナルに入る。
| 1 2 | >docker exec -it postgres /bin/bash root@68f874da7378:/# ls | 
PostgreSQLのターミナルに入る。
| 1 2 3 4 | >docker exec -it postgres /bin/bash root@f6c41b769e43:/# psql -U postgres -d postgres psql (16.1 (Debian 16.1-1.pgdg120+1)) Type "help" for help. | 
データベースの確認。
| 1 2 3 4 5 6 7 8 9 10 | postgres=# \l                                                       List of databases    Name    |  Owner   | Encoding | Locale Provider |  Collate   |   Ctype    | ICU 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) | 
テーブル作成。
| 1 2 3 4 5 6 7 | postgres=# CREATE TABLE employees ( postgres(#   id SERIAL PRIMARY KEY, postgres(#   name VARCHAR(20), postgres(#   age INT postgres(# ); CREATE TABLE p | 
作成されたテーブルを確認。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | postgres=# \d                 List of relations  Schema |       Name       |   Type   |  Owner --------+------------------+----------+----------  public | employees        | table    | postgres  public | employees_id_seq | sequence | postgres (2 rows) postgres=# \d employees                                    Table "public.employees"  Column |         Type          | Collation | Nullable |                Default --------+-----------------------+-----------+----------+---------------------------------------  id     | integer               |           | not null | nextval('employees_id_seq'::regclass)  name   | character varying(20) |           |          |  age    | integer               |           |          | Indexes:     "employees_pkey" PRIMARY KEY, btree (id) | 
レコード操作確認。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | postgres=# INSERT INTO employees (name, age) VALUES ('Eric', 28); INSERT 0 1 postgres=# INSERT INTO employees (name, age) VALUES ('Bob', 32); INSERT 0 1 postgres=# INSERT INTO employees (name, age) VALUES ('Tim', 45); INSERT 0 1 postgres=# SELECT * FROM employees;  id | name | age ----+------+-----   1 | Eric |  28   2 | Bob  |  32   3 | Tim  |  45 (3 rows) | 
なお、今のマシンにはWindows版のPostgreSQLもインストールしているので、直接コンソールからpsqlも使える。ただしバージョンがコンテナ上のサーバーとWindows上のpsqlで異なるので警告が出る。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | >psql -h localhost -p 5431 -U postgres -d postgres ユーザ postgres のパスワード: psql (12.15、サーバ 16.1 (Debian 16.1-1.pgdg120+1)) 警告: psql のメジャーバージョンは 12 ですが、サーバのメジャーバージョンは 16 です。          psql の機能の中で、動作しないものがあるかもしれません。 "help"でヘルプを表示します。 postgres=# \l                                                       List of databases    Name    |  Owner   | Encoding | Locale Provider |  Collate   |   Ctype    | ICU 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) postgres=# \d                 List of relations  Schema |       Name       |   Type   |  Owner --------+------------------+----------+----------  public | employees        | table    | postgres  public | employees_id_seq | sequence | postgres (2 rows) postgres=# SELECT * FROM employees;  id | name | age ----+------+-----   1 | Eric |  28   2 | Bob  |  36   3 | Tim  |  45 (3 rows) | 
タイムゾーン
タイムゾーンを確認するとUTCになっている。
| 1 2 3 4 5 | postgres=# show timezone;  TimeZone ----------  Etc/UTC (1 row) | 
Docker Composeでタイムゾーンを設定するには、以下の様に環境変数をセットする。
| 1 2 | environment:   TZ: "Asia/Tokyo" |