OSSDB標準教科書11章 環境構築

PostgreSQLRDB勉強メモ

OSSDB標準教科書

[http://oss-db.jp/ossdbtext/text.shtml:embed:cite]

環境

  • Win10
  • docker

11章 導入

  • 1章開幕で「導入は付録A」といきなり付録に誘導されるうえ、付録Aとやらが見つからない
  • たぶんこの11章のことでしょう
  • 導入部分は結局参考にならず

インストール

VOLUME /var/lib/postgresql/data

ここにデータを保存するようになっているらしいので、ホスト側と共有する

データ永続化用ボリューム作る

Docker for Windows で postgres コンテナの Volume マウントを安全にする

permission deniedで困ってたんですよ

cmd

docker volume create postgresql_data

postgresコンテナ起動・入る

イメージがなければDockerhubから公式イメージがpullされる

 docker run -it -v postgresql_data:/var/lib/postgresql/data --user=postgres postgres bash

初期設定(1度だけやるやつ)

初期化

postgres@b6565e08d814:/$ cd /usr/lib/postgresql/11/bin/
postgres@b6565e08d814:/usr/lib/postgresql/11/bin/$ ./initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    ./pg_ctl -D /var/lib/postgresql/data -l logfile start

サーバ起動

とりあえずログなしで始めてみる

bash

postgres@b6565e08d814:/usr/lib/postgresql/11/bin/$ ./pg_ctl -D ~/data/ start
waiting for server to start....2019-01-17 23:40:28.250 UTC [22] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-01-17 23:40:28.250 UTC [22] LOG:  listening on IPv6 address "::", port 5432
2019-01-17 23:40:28.365 UTC [22] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-01-17 23:40:28.558 UTC [23] LOG:  database system was shut down at 2019-01-17 23:39:53 UTC
2019-01-17 23:40:28.619 UTC [22] LOG:  database system is ready to accept connections
 done
server started

11.5 動作の確認

bash

psql -l

なるほどね

                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 postgres  | 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
(3 rows)

11.7 データベースの作成

bash

createdb ossdb
psql -l
    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
 -----------+----------+----------+------------+------------+-----------------------
+ ossdb     | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
  postgres  | 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)

接続確認

bash

psql ossdb
psql (11.1 (Debian 11.1-1.pgdg90+1))
Type "help" for help.

ossdb=#

11.7.5 データがちゃんとホスト側に永続化されていることを確認

いったんコンテナを抜けて解体して入り直す

ハッシュが変わっている = 異なるコンテナ

bash

postgres@320b7437705b:/$ /usr/lib/postgresql/11/bin/pg_ctl -D ~/data start
postgres@320b7437705b:/$ psql -l

さっき作ったossdbがちゃんといる。OK

                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
-----------+----------+----------+------------+------------+-----------------------
 ossdb     | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
 postgres  | 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)

11.8 表の作成

CREATE TABLE prod
(prod_id   integer,
 prod_name text,
 price     integer);

CREATE TABLE customer
(customer_id integer,
 customer_name text);

CREATE TABLE orders
(order_id integer,
 order_date timestamp,
 customer_id integer,
 prod_id integer,
 qty integer);

11.9 データの入力

INSERT INTO customer(customer_id, customer_name) VALUES (1, '佐藤商事');
INSERT INTO customer(customer_id, customer_name) VALUES (2, '鈴木物産');
INSERT INTO customer(customer_id, customer_name) VALUES (3, '高橋商店');

INSERT INTO prod(prod_id, prod_name, price) VALUES (1, 'みかん', 50);
INSERT INTO prod(prod_id, prod_name, price) VALUES (2, 'りんご', 70);
INSERT INTO prod(prod_id, prod_name, price) VALUES (3, 'メロン', 100);

INSERT INTO orders(order_id, order_date, customer_id, prod_id, qty) VALUES (1,now(),1,1,10);
INSERT INTO orders(order_id, order_date, customer_id, prod_id, qty) VALUES (2,now(),2,2,5); 
INSERT INTO orders(order_id, order_date, customer_id, prod_id, qty) VALUES (3,now(),3,3,8); 
INSERT INTO orders(order_id, order_date, customer_id, prod_id, qty) VALUES (4,now(),2,1,3); 
INSERT INTO orders(order_id, order_date, customer_id, prod_id, qty) VALUES (5,Now(),3,2,4);