PostgreSQLでデータベースとユーザを作成する
0.改訂履歴
- 2005. 新規作成
1.はじめに
このドキュメントでは,PostgreSQL7.4.6にて,データベースを作成する手順を説明する. PostgreSQLでは,外部コマンドのcreatedbコマンドを利用する方法と,他のデータベース製品と同じくcreate database文を用いてSQLのDDLを使う方法があるが,ここではデータベースインストール後の1回目ということもあり,createdbコマンドを用いて作成する方法について説明する.
2.create databaseを実行する
- 現在のユーザを確認する.
[postgres@mars postgres]$ id uid=501(postgres) gid=503(postgres) groups=503(postgres) [postgres@mars postgres]$ |
- createdbコマンドを用いて,データベースを作成する.
- ここでは,EUCコードを使い,MyTestDBという名前のデータベースを作成する.
- なお,利用するデータベースユーザは,postgresというPostgreSQL上のスーパーユーザである.
[postgres@mars postgres]$ /usr/local/pgsql/bin/createdb -E EUC_JP ¥ > -U postgres MyTestDB CREATE DATABASE [postgres@mars postgres]$ |
- データベースユーザpostgresにパスワードを設定している場合は,-Wオプションを用いて設定して実行する.
3.データベースユーザを作成する
- 作成したMyTestDBの所有者として設定するためのデータベースユーザ"steve"を作成する.
- steveユーザには,create database権限は与えるが,新たにユーザを作成する権限は設定しない.
- createuserコマンドを用いて,実行する.
[postgres@mars postgres]$ /usr/local/pgsql/bin/createuser steve Shall the new user be allowed to create databases? (y/n) y Shall the new user be allowed to create more new users? (y/n) n CREATE USER [postgres@mars postgres]$ |
- 対話的にオプションを設定比内場合は,次のようにオプションを設定する.
[postgres@mars postgres]$ /usr/local/pgsql/bin/createuser ¥ > --createdb --no-adduser bill CREATE USER [postgres@mars postgres]$ |
- createuserコマンドで利用できるパラメータは次の通り.
[postgres@mars postgres]$ /usr/local/pgsql/bin/createuser -help createuser creates a new PostgreSQL user. Usage: createuser [OPTION]... [USERNAME] Options: -a, --adduser user can add new users -A, --no-adduser user cannot add new users -d, --createdb user can create new databases -D, --no-createdb user cannot create databases -P, --pwprompt assign a password to new user -E, --encrypted encrypt stored password -N, --unencrypted do no encrypt stored password -i, --sysid=SYSID select sysid for new user -e, --echo show the commands being sent to the server -q, --quiet don't write any messages --help show this help, then exit --version output version information, then exit Connection options: -h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port -U, --username=USERNAME user name to connect as (not the one to create) -W, --password prompt for password to connect If one of -a, -A, -d, -D, and USERNAME is not specified, you will be prompted interactively. Report bugs to <pgsql-bugs@postgresql.org>. [postgres@mars postgres]$ |
- データベースの中で,作成したユーザが登録されているか確認する.
[postgres@mars postgres]$ psql -U steve MyTestDB Welcome to psql 7.4.6, the PostgreSQL interactive terminal. Type: ¥copyright for distribution terms ¥h for help with SQL commands ¥? for help on internal slash commands ¥g or terminate with semicolon to execute query ¥q to quit MyTestDB=> ¥du List of database users User name | User ID | Attributes -----------+---------+---------------------------- bill | 101 | create database postgres | 1 | superuser, create database steve | 100 | create database (3 rows) MyTestDB=> |