PostgreSQL 基本的コマンド

2018年5月17日 Posted by PURGE

■ postgres ユーザでログイン

> psql -U postgres
ユーザー postgres のパスワード:
psql (10.4)
"help" でヘルプを表示します。

■ PostgreSQLのバージョン確認

postgres=# select version();
                          version
------------------------------------------------------------
 PostgreSQL 10.4, compiled by Visual C++ build 1800, 64-bit
(1 行)

■ データベース一覧表示

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 行)

■ データベース作成

postgres=# create database sample_db;
CREATE DATABASE

■ データベース一覧表示

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 行)

■ データベース削除

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 行)

■ データベース作成 with ユーザ

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

コメントを残す

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