Closed masayuki14 closed 6 years ago
show create table [table_name]
はpostgresでどうやるんだろう。
テーブル情報の詳細
\d [table_name]
詳細な情報
$ pg_dump interest_tier3_dev -Upostgres -s -t tripadvisor_businesses
create table sample(
id bigint,
name text
);
テーブル作ってみる。
create table sample2 (
id bigint PRIMARY KEY,
name text
);
Insertはいつもの感じ set column = value
はできないぽい
# insert into sample values(1, 'name');
# insert into sample (id) values (3);
# select * from sample;
id | name
----+------
1 | name
3 |
(2 rows)
primary key 制約
# insert into sample2 (name) values ('summer');
ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, summer).
# insert into sample2 values (4, 'summer wars');
INSERT 0 1
# insert into sample2 values (4, 'summer wars 2');
ERROR: duplicate key value violates unique constraint "sample2_pkey"
DETAIL: Key (id)=(4) already exists.
既存のテーブルに PRIMARY KEY
を設定するには
制約の追加 https://www.postgresql.jp/document/9.6/html/ddl-alter.html#ddl-alter-adding-a-constraint
alter table add [key]
でいけるぽいな
# \d sample;
Table "public.sample"
Column | Type | Modifiers
--------+--------+-----------
id | bigint |
name | text |
# alter table sample add primary key (id);
ALTER TABLE
# \d sample;
Table "public.sample"
Column | Type | Modifiers
--------+--------+-----------
id | bigint | not null
name | text |
Indexes:
"sample_pkey" PRIMARY KEY, btree (id)
# alter table data_table add primary key (place_id_d);
ERROR: could not create unique index "data_table_pkey"
DETAIL: Key (place_id=(1867744) is duplicated.
重複あってエラー
# truncate table data_table
truncate
も同じ。消した後なちゃんとPrimaryKey追加できた。
PK 設定したので mode: merge
で embluk
うごかす。
embluk run config.yml
2回動かしてもエラーでない 😄
データ型 date
time
timestamp
3つ
interest_tier3_dev=# select * From sample;
id | name | created_at | created_date | created_time | created_timestamp
----+------+------------+--------------+-----------------+----------------------------
1 | name | | | |
3 | | | 2018-02-23 | 05:26:04.908167 | 2018-02-23 05:26:10.739471
(2 rows)
interest_tier3_dev=# \d sample
Table "public.sample"
Column | Type | Modifiers
-------------------+-----------------------------+-----------
id | bigint | not null
name | text |
created_at | time without time zone |
created_date | date |
created_time | time without time zone |
created_timestamp | timestamp without time zone |
Indexes:
"sample_pkey" PRIMARY KEY, btree (id)
日付は timestamp
型でいこう。
トリガーを試してみたい。こればっかりは GUI ツール使ったほうが良さそうだ。
ツール。
pgadmin で Trigger Function
を登録してみた。
GUIがないとどうにも。
function名 : trip_insert_trigger
SQL で確認する。
# select prosrc from pg_proc where proname = 'trip_insert_trigger' limit 1 ;
prosrc
----------------------------------------
+
-- RETURNS trigger +
+
BEGIN +
NEW.created_at := CURRENT_TIMESTAMP;+
NEW.updated_at := CURRENT_TIMESTAMP;+
RETURN NEW; +
END; +
(1 row)
登録されている
https://docs.docker.com/engine/reference/builder/#entrypoint
docker-compose.ml
にworking_dir
entrypoint
を指定できるぜ!
docker-compose.yml
のポスグレのところに entrypoint:
で create table のシェル実行するようにしたけど、ポスグレの起動でエラーが出てる。
https://qiita.com/hirosemi/items/199a3c753130c12d52f1
ポスグレの公式Imageでは /docker-entrypoint.sh
が実行されるらしい。
だから entrypoint
を上書きしちゃうといけない。
中を除く
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
シェルにしなくても、SQLを /docker-entrypoint-initdb.d
に置けば起動時に実行してくれる。
docker-entrypoint-initdb.d
はDBのユーザーとかの設定と同じで初めて実行した時しか動かないので注意。
create table を自動化したい。