「PostgreSQLでよくやること」の版間の差分
提供: オレッジベース
42行目: | 42行目: | ||
作成 | 作成 | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
− | # | + | # CREATE DATABASE <DABASE_NAME>; |
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
− | # | + | # CREATE DATABASE <DABASE_NAME> OWNER <OWNER_NAME>; |
</syntaxhighlight> | </syntaxhighlight> | ||
削除 | 削除 | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
− | # | + | # DROP DATABASE <DATABASE_NAME>; |
</syntaxhighlight> | </syntaxhighlight> | ||
62行目: | 62行目: | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
# \dt | # \dt | ||
+ | </syntaxhighlight> | ||
+ | 作成 | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | # CREATE TABLE IF NOT EXISTS <TABLE_NAME>( | ||
+ | id BIGSERIAL, | ||
+ | <COLUMN_NAME> <TYPE>, | ||
+ | ..., | ||
+ | PRIMARY KEY (id) | ||
+ | ); | ||
+ | COMMENT ON COLUMN <TABLE_NAME>.id IS 'ID'; | ||
+ | COMMENT ON COLUMN <TABLE_NAME>.<COLUMN_NAME>IS '<COMMENT>'; | ||
+ | ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
削除 | 削除 | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
− | # | + | # DROP TABLE <TABLE_NAME>; |
</syntaxhighlight> | </syntaxhighlight> | ||
77行目: | 89行目: | ||
確認 | 確認 | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
− | # | + | # SELECT current_schema; |
</syntaxhighlight> | </syntaxhighlight> | ||
変更 | 変更 | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
− | # | + | # SET search_path TO <SCHEMA_NAME>; |
</syntaxhighlight> | </syntaxhighlight> | ||
2017年12月14日 (木) 13:21時点における版
dump/restore
dump
$ pg_dump <DATABASE_NAME> > <FILE_NAME>
restore
$ psql <DATABASE_NAME>< infile
role
一覧
# \du
作成
# CREATE ROLE <ROLE_NAME> WITH LOGIN PASSWORD '<PASSWORD>';
名前変更
# ALTER ROLE <OLD_ROLE_NAME>TO <NEW_ROLE_NAME>
削除
# DROP ROLE <ROLE_NAME>;
database
一覧
# \l
切り替え
# \c <DABASE_NAME>
作成
# CREATE DATABASE <DABASE_NAME>;
# CREATE DATABASE <DABASE_NAME> OWNER <OWNER_NAME>;
削除
# DROP DATABASE <DATABASE_NAME>;
table
一覧(viewとかも)
# \d
一覧(tableだけ)
# \dt
作成
# CREATE TABLE IF NOT EXISTS <TABLE_NAME>(
id BIGSERIAL,
<COLUMN_NAME> <TYPE>,
...,
PRIMARY KEY (id)
);
COMMENT ON COLUMN <TABLE_NAME>.id IS 'ID';
COMMENT ON COLUMN <TABLE_NAME>.<COLUMN_NAME>IS '<COMMENT>';
...
削除
# DROP TABLE <TABLE_NAME>;
schema
一覧
# \dn
確認
# SELECT current_schema;
変更
# SET search_path TO <SCHEMA_NAME>;