PostgreSQLの実験
Modified: 20 July 2003, 4 August 2002
RedHat Linux7.3 に同梱されているPostgreSQLを動作させて見ます。
一年ぶりに見直して、少し訂正しました。(20 July 2003)
PostgreSQLの小技 (20 July 2003)
PostgreSQLの起動と初期化 (28 July 2002)
ユーザ登録とデータベースの作成 (28 July 2002)
PostgreSQLを起動する
「システム」をクリックして、「起動とシャットダウン」から「PostgreSQL」を選び、「いますぐ起動」ボタンで起動します。
ユーザ登録とパスワード
PostgreSQLには、"postgres"というユーザーの登録と、パスワードの設定が必須です。
RedHatLinux7.3 では、インストール後には登録されていますので、パスワードの設定のみ行います。
パスワードは、"root" になって、"passwd" コマンドで行います。
$ su
Password: ********
# passwd postgres
Changing password for user postgres.
New password: *******
Retype new password: *******
passwd: all authentication tokens updated successfully.
#初期状態では、パスワードは "なし" になっていますので、変更ではなくて、新規に設定することになります。
環境変数の設定
環境変数は、すべてのユーザに必要ですが、まず、先に作った "postgres"というユーザになり、カレントディレクトリを、"/var/lib/pgsql" に移動します。
$ su postgres
Password: *******
bash-2.05a$ cd /var/lib/pgsql
bash-2.05a$そして、".bash_profile"に、青い部分を追加します。
PGDATA=/var/lib/pgsql/data
[ -f $PGDATA/../initdb.i18n ] && source $PGDATA/../initdb.i18n
export PGDATA
export POSTGRES_HOME=/var/lib/pgsql
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/data
export MANPATH="$MANPATH":$POSTGRES_HOME/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"
データベースの初期化
initdb でデーターベースを初期化します。
bash-2.05a$ initdb --encoding=EUC_JP
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
Fixing permissions on existing directory /var/lib/pgsql/data... ok
creating directory /var/lib/pgsql/data/base... ok
creating directory /var/lib/pgsql/data/global... ok
creating directory /var/lib/pgsql/data/pg_xlog... ok
creating directory /var/lib/pgsql/data/pg_clog... ok
creating template1 database in /var/lib/pgsql/data/base/1... ok
creating configuration files... ok
initializing pg_shadow... ok
enabling unlimited row size for system tables... ok
creating system views... ok
loading pg_description... ok
vacuuming database template1... ok
copying template1 to template0... ok
Success. You can now start the database server using:
/usr/bin/postmaster -D /var/lib/pgsql/data
or
/usr/bin/pg_ctl -D /var/lib/pgsql/data -l logfile start
bash-2.05a$
postmasterの起動
initdb を実行した後に出てくるメッセージどおり、以下のように実行すると、postgreSQLが起動します。
$ /usr/bin/pg_ctl -D /home/tomo/pgsql/data -l logfile start
postmaster successfully started
$
"postgres"でログイン
PostgreSQLの管理は、"postgres"というユーザで行いますので、"postgres"でログインします。
| $ su postgres Password: ********* % |
以下、"postgres"のユーザのプロンプトは、"%"で表記します。
ユーザの作成
| % createuser tomo 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 % |
ユーザの削除
| % dropuser tomo DROP USER % |
データベースの作成
| % createdb testdb CREATE DATABASE % |
データベースの削除
| % dropdb testdb DROP DATABASE % |
DBを確認する
以下のコマンドで、登録されたことが確認できます。
% psql -l
List of databases Name
| Owner
| Encoding
----------- +---------- +----------- template0 | postgres | SQL_ASCII template1 | postgres | SQL_ASCII testdb | postgres | SQL_ASCII (3 rows) %
データを登録してみる
データベースを指定して、"psql"を起動します。
% psql testdb
Welcome to psql, 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
testdb=#データを登録します。
testdb=# create table addr (name text,address text);
CREATE
testdb=# insert into addr values('tomo','nara');
INSERT 277834 1
testdb=# insert into addr values('hiro','osaka');
INSERT 277835 1
testdb=#登録した内容をすべて表示してみます。
testdb=# select * from addr;
name | address
------+---------
tomo | nara
hiro | osaka
(2 rows)
testdb=#終了は、"\q"で行います。
testdb=# \q
%