こんにちは、やすです。
今回は、PostgreSQLで使うユーザの作成と削除のやり方を紹介します。
PostgreSQLをインストールすると、「postgres」というPostgreSQLの操作は何でもできるユーザーが自動で作られます。
全員が常に「postgres」ユーザーを使う運用だとかなり心配なので、DML(テーブルに入っているデータ操作:select、insert、delete、update)だけできるユーザーやDDL(テーブルの作成や削除)やDCL(データベースの作成や削除)もできるユーザーなど分ける必要があります。
ユーザーの作成・削除
事前確認
(1)postgresユーザで、postgresデータベースに接続する
-bash-4.2$ psql postgres
psql (9.2.24)
"help" でヘルプを表示します.
postgres=#
(2)ユーザーの確認
postgres=# \du ロール一覧
ロール名 | 属性 | メンバー
----------+----------------------------------------------------------------------+----------
postgres | スーパーユーザ, ロールを作成できる, DBを作成できる, レプリケーション | {}
postgres=#
ユーザーの作成
(3)ユーザー作成
postgres=# create user tanaka;
CREATE ROLE
postgres=#
事後確認
さっそく色々確認してみましょう。
(4)田中さんユーザ作成できましたね!
postgres=# \du
ロール一覧
ロール名 | 属性 | メンバー
----------+----------------------------------------------------------------------+----------
postgres | スーパーユーザ, ロールを作成できる, DBを作成できる, レプリケーション | {}
tanaka | | {}
postgres=#
(5)一度田中さんで繋げてみましょう。
postgres=# \q
-bash-4.2$
-bash-4.2$ psql -U tanaka -d postgres
psql (9.2.24)
"help" でヘルプを表示します.
postgres=>
(6)田中さんで繋げていることを確認
postgres=> select current_user;
current_user
--------------
tanaka
(1 行)
postgres=>
※一般ユーザだとプロンプト表示が「postgres=#」ではなくて「postgres=>」ですね。
(7)DCL系の操作はできないですね。
postgres=> create user suzuki;
ERROR: permission denied to create role
postgres=>
(8)DDL系の操作はできる。
postgres=> CREATE TABLE CardInfo2 (
postgres(> CardID nchar(6),
postgres(> CustomerID nchar(5),
postgres(> EmployeeID int
postgres(> );
CREATE TABLE
postgres=>
postgres=> \d
リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+-----------+----------+----------
public | cardinfo | テーブル | postgres
public | cardinfo2 | テーブル | tanaka
(2 行)
postgres=>
(9)田中さんが所有者のテーブルができた
postgres=> \d
リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+-----------+----------+----------
public | cardinfo | テーブル | postgres
public | cardinfo2 | テーブル | tanaka
(2 行)
postgres=>
まとめ
今回は、ユーザーの作成と削除をご紹介しました。
ではまた、バイバーイ♪