PostgreSQL 基本的コマンド

■ postgres ユーザでログイン
[bash]
> psql -U postgres
ユーザー postgres のパスワード:
psql (10.4)
"help" でヘルプを表示します。
[/bash]

■ PostgreSQLのバージョン確認
[bash]
postgres=# select version();
version
————————————————————
PostgreSQL 10.4, compiled by Visual C++ build 1800, 64-bit
(1 行)

[/bash]

■ データベース一覧表示
[bash]
postgres=# \l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限
———–+———-+——————+——————–+——————–+———————–
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行)
[/bash]

■ データベース作成
[bash]
postgres=# create database sample_db;
CREATE DATABASE
[/bash]

■ データベース一覧表示
[bash]
postgres=# \l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限
———–+———-+——————+——————–+——————–+———————–
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
sample_db | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行)
[/bash]

■ データベース削除
[bash]
postgres=# drop database sample_db;
DROP DATABASE
postgres=# \l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限
———–+———-+——————+——————–+——————–+———————–
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行)
[/bash]

■ データベース作成 with ユーザ
[bash]
postgres=# create database sample_db owner sample;
ERROR: ロール"sample"は存在しません
postgres=# create user sample;
CREATE ROLE
postgres=# create database sample_db owner sample;
CREATE DATABASE
postgres=# alter role sample with password ‘password’;
ALTER ROLE
postgres=# alter role sample with createdb;
ALTER ROLE
[/bash]

This entry was posted in PostgreSQL, 技術情報. Bookmark the permalink.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です