yum でインストール直後の PostgreSQL 最初の一歩として、
- PostgreSQL スーパーユーザー postgres にパスワード設定
- PostgreSQL 内のユーザ testuser 作成
- PostgreSQL 用の Linux ユーザ testuser 作成
- PostgreSQL 内のユーザ testuser 用のデータベース testdb 作成
- データベース testdb、PostgreSQL ユーザ testuser、Linux ユーザ testuser の削除
をやってみようと思います。
PostgreSQL のプロンプトにログイン
1 2 3 4 5 6 7 8 | [root@localhost ~] # PostgreSQL インストールで自動で作成された PostgreSQL のスーパーユーザー postgres に変更 [root@localhost ~] # su - postgres - bash -3.2$ PostgreSQL のプロンプトへ接続(インストール時はパスワードが不要) - bash -3.2$ psql psql (9.2.3) "help" でヘルプを表示します. postgres= # |
ユーザーへのパスワード設定
1 2 3 4 | postgres=# # スーパーユーザー postgres にパスワード設定 postgres=# ALTER USER postgres WITH PASSWORD 'kokoni_password' ; ALTER ROLE postgres=# |
確認しようと一旦ログアウトして、「su – postgres」→「psql」と進みましても、パスワードの入力を求められませんでした。。。ナンデ?><
新しく PostgreSQL のユーザ作成、パスワード設定
1 2 3 4 5 6 7 8 9 10 | - bash -3.2$ # 新しく PostgreSQL のユーザを作成し、パスワードを設定 - bash -3.2$ # -d データベース作成を許可 - bash -3.2$ # -P パスワードを設定 - bash -3.2$ createuser -d -P testuser 新しいロールのためのパスワード: もう一度入力してください: - bash -3.2$ exit logout [root@localhost ~] # useradd testuser [root@localhost ~] # |
「psql」でプロンプトに接続し、「¥du」でユーザが作成されとことと「select usename,passwd from pg_shadow;」でパスワードが設定されていることが確認できました。 それにしても、PostgreSQL のユーザ以外に、同名で Linux のユーザも作成する必要があるのが面倒です。何とかならないものでしょうか。。。
新しく作った PostgreSQL ユーザ用のデータベースを作成
1 2 3 4 5 6 7 8 9 | [root@localhost ~] # testuser ユーザ用の PostgreSQL のデータベース作成 [root@localhost ~] # su - testuser [testuser@localhost ~]$ createdb testdb [testuser@localhost ~]$ # testuser ユーザで testdb に接続 [testuser@localhost ~]$ psql testdb psql (9.2.3) "help" でヘルプを表示します. testdb=> |
データベース作成には、Linux ユーザを変更する必要がありました。
作成後、そのデータベースへ接続してみました。 これも、面倒です。Linux で PostgreSQL 用のユーザに変更してからそのユーザ用に PostgreSQL のデータベースを作る感覚が馴染めません。今まで MySQL では、MySQL にログインしてから MySQL 用のユーザを作り、データベースを作り、、、と全て MySQL 内で完結していましたのに、PostgreSQL では Linux と混じっているのがどうにもヘンな気持ちです。
データベース削除、PostgreSQL ユーザ削除、Linux ユーザ削除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [testuser@localhost ~]$ # PostgreSQL データベース削除 [testuser@localhost ~]$ dropdb testdb [testuser@localhost ~]$ [testuser@localhost ~]$ # PostgreSQL ユーザ削除 [testuser@localhost ~]$ exit logout [root@localhost ~] # su - postgres - bash -3.2$ dropuser testuser - bash -3.2$ - bash -3.2$ exit logout [root@localhost ~] # # Linux ユーザ削除 [root@localhost ~] # userdel -r testuser [root@localhost ~] # |
やはり PostgreSQL ユーザと Linux ユーザの2つ削除しなければならないのが面倒です。
ユーザ一覧、ユーザパスワード設定、データベース一覧の確認コマンド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | postgres- # # ユーザ一覧の確認 postgres- # \du ロール一覧 ロール名 | 属性 | メンバー ----------+----------------------------------------------------------------------+---------- postgres | スーパーユーザ, ロールを作成できる, DBを作成できる, レプリケーション | {} testuser | DBを作成できる | {} postgres- # postgres- # # ユーザパスワード設定の確認 postgres= # select usename,passwd from pg_shadow; usename | passwd ----------+------------------------------------- postgres | md52356ad72626bd0f77e9dab55f1423abd testuser | md599e5ea7a6f7c3269995cba3927fd0093 (2 行) postgres= # \q - bash -3.2$ # データベース一覧を確認 - bash -3.2$ psql -l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権 -----------+----------+------------------+-------------+-------------------+----------------------- postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c /postgres + | | | | | postgres=CTc /postgres template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c /postgres + | | | | | postgres=CTc /postgres testdb | testuser | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | (4 行) - bash -3.2$ |
今まで試してきたものと時系列は前後しますけれども、一覧を確認するコマンドです。それぞれ作成、削除の前後でしっかり確認したいものですね♪
おわりに
本投稿は、次の投稿の続きに位置しております。
やってみて感じたことを述べます。コマンド自体にむずかしいところはないのですけれども、考え方が難しいです。つまり、Linux ユーザを作成しておいて、それと同名で PostgreSQL ユーザを作成する考え方がわかりません。PostgreSQL のユーザ数だけ Linux ユーザも作成しなければならないのは、とても面倒です。
なぜ?Linux ユーザを作成しないで済む方法はないものでしょうか?
次は PHP と PostgreSQL との連携と考えておりますが、その前に Linux ユーザを作成しない方法はないか探していそうな気がします。
それはともかく、参考にさせていただいたページです!ありがとうございます!
以上です。
「【不満】PostgreSQL 最初の一歩!ユーザと DB 作成!確認!【CentOS 5.9】【実際ニュービー】」への2件の返信
[…] 【不満】PostgreSQL 最初の一歩!ユーザと DB 作成!確認!【CentOS 5.9】【実際ニュービー】 | oki2a24 […]
[…] 【不満】PostgreSQL 最初の一歩!ユーザと DB 作成!確認!【CentOS 5.9】【実際ニュービー】 | oki2a24 […]