「PostgreSQLでよくやること」の版間の差分
提供: オレッジベース
(→database) |
|||
(同じ利用者による、間の1版が非表示) | |||
106行目: | 106行目: | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
# DROP TABLE IF EXISTS <TABLE_NAME>; | # DROP TABLE IF EXISTS <TABLE_NAME>; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | == column == | ||
+ | === 追加 === | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | # ALTER TABLE <TABLE_NAME> ADD COLUMN <COLUMN_NAME> <COLUMN_TYPE>; | ||
+ | </syntaxhighlight> | ||
+ | or 特定のカラムの後に追加 | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | # ALTER TABLE <TABLE_NAME> ADD COLUMN <COLUMN_NAME> <COLUMN_TYPE> AFTER <EXISTING_COLUMN_NAME>; | ||
+ | </syntaxhighlight> | ||
+ | === 型変更 === | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | # ALTER TABLE <TABLE_NAME> ALTER COLUMN <COLUMN_NAME> TYPE <NEW_COLUMN_TYPE>; | ||
+ | </syntaxhighlight> | ||
+ | === 削除 === | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | # ALTER TABLE <TABLE_NAME> DROP COLUMN <COLUMN_NAME>; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
137行目: | 156行目: | ||
# ALTER SCHEMA <OLD_NAME> RENAME TO <NEW_NAME>; | # ALTER SCHEMA <OLD_NAME> RENAME TO <NEW_NAME>; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
=== search_path変更 === | === search_path変更 === | ||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
154行目: | 172行目: | ||
# DROP SCHEMA <SCHEMA_NAME> CASCADE; | # DROP SCHEMA <SCHEMA_NAME> CASCADE; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | == ほか == | ||
+ | === 複数カラムの組み合わせで重複しているレコードを抽出 === | ||
+ | <syntaxhighlight lang="sql"> | ||
+ | SELECT | ||
+ | * | ||
+ | FROM | ||
+ | <TABLE> | ||
+ | WHERE | ||
+ | (<COLUMN_1>, <COLUMN_2>) IN ( | ||
+ | SELECT DISTINCT | ||
+ | <COLUMN_1> | ||
+ | , <COLUMN_2> | ||
+ | FROM | ||
+ | <TABLE> | ||
+ | GROUP BY | ||
+ | <COLUMN_1> | ||
+ | , <COLUMN_2> | ||
+ | HAVING | ||
+ | count(<COLUMN_1>) > 1 | ||
+ | AND count(<COLUMN_2>) > 1 | ||
+ | ) | ||
+ | ORDER BY | ||
+ | <COLUMN_1> | ||
+ | , <COLUMN_2> | ||
+ | ; | ||
+ | </syntaxhighlight> | ||
+ | |||
2021年2月2日 (火) 15:25時点における最新版
dump/restore
dump
$ pg_dump <DATABASE_NAME> > <FILE_NAME>
or
$ pg_dump -Fc <DATABASE_NAME> > <FILE_NAME>
or schema指定
$ pg_dump -n <SCHEMA> <DATABASE_NAME> > <FILE_NAME>
restore
$ psql <DATABASE_NAME> < <FILE_NAME>
or pg_dumpでFcやFtをつけてたらこっち
$ pg_restore -d <DATABASE_NAME> <FILE_NAME>
role
一覧
# \du
作成
# CREATE ROLE <ROLE_NAME> WITH LOGIN PASSWORD '<PASSWORD>';
名前変更
# ALTER ROLE <OLD_ROLE_NAME> TO <NEW_ROLE_NAME>
パスワード
変更
# ALTER ROLE <ROLE_NAME> WITH PASSWORD '<PASSWORD>';
削除
# ALTER ROLE <ROLE_NAME> WITH PASSWORD NULL;
削除
# DROP ROLE <ROLE_NAME>;
database
一覧
# \l
切り替え
# \c <DATABASE_NAME>
作成
$ createdb -U <ROLE_NAME> <DATABASE_NAME>
or
# CREATE DATABASE <DATABASE_NAME>;
or
# CREATE DATABASE <DATABASE_NAME> OWNER <ROLE_NAME>;
名前変更
# ALTER DATABASE <OLD_NAME> RENAME TO <NEW_NAME>;
削除
$ dropdb -U <ROLE_NAME> <DATABASE_NAME>
or
# 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 IF EXISTS <TABLE_NAME>;
column
追加
# ALTER TABLE <TABLE_NAME> ADD COLUMN <COLUMN_NAME> <COLUMN_TYPE>;
or 特定のカラムの後に追加
# ALTER TABLE <TABLE_NAME> ADD COLUMN <COLUMN_NAME> <COLUMN_TYPE> AFTER <EXISTING_COLUMN_NAME>;
型変更
# ALTER TABLE <TABLE_NAME> ALTER COLUMN <COLUMN_NAME> TYPE <NEW_COLUMN_TYPE>;
削除
# ALTER TABLE <TABLE_NAME> DROP COLUMN <COLUMN_NAME>;
schema
一覧
# \dn
確認
# SELECT current_schema;
作成
# CREATE SCHEMA <SCHEMA_NAME>;
複製
$ pg_dump -n <SOURCE_SCHEMA_NAME> <DATABASE_NAME> > <FILE_NAME>
# ALTER SCHEMA <NEW_SCHEMA_NAME> RENAME TO <SOURCE_SCHEMA_NAME>;
$ psql <DATABASE_NAME> < <FILE_NAME>
名前変更
# ALTER SCHEMA <OLD_NAME> RENAME TO <NEW_NAME>;
search_path変更
# SET search_path TO <SCHEMA_NAME>;
or
# ALTER USER <ROLE_NAME> SET search_path TO <SCHEMA_NAME>;
削除
# DROP SCHEMA <SCHEMA_NAME>;
or スキーマ内のテーブルごと削除
# DROP SCHEMA <SCHEMA_NAME> CASCADE;
ほか
複数カラムの組み合わせで重複しているレコードを抽出
SELECT
*
FROM
<TABLE>
WHERE
(<COLUMN_1>, <COLUMN_2>) IN (
SELECT DISTINCT
<COLUMN_1>
, <COLUMN_2>
FROM
<TABLE>
GROUP BY
<COLUMN_1>
, <COLUMN_2>
HAVING
count(<COLUMN_1>) > 1
AND count(<COLUMN_2>) > 1
)
ORDER BY
<COLUMN_1>
, <COLUMN_2>
;