初期セットアップと確認
1.実行ユーザー、データーベースの設定について
yumで導入した場合、サーバを実行するユーザーは自動的に作成されています。また、PostgreSQLを最初に起動すると、テンプレートデータベースが作成されます。ユーザー名は「postgres」、テンプレートデータベースは「template0」です。データベースの格納先は、デフォルトで「/var/lib/pgsql/data」以下になります。
2.データベースサービスを開始する
ターミナルより、PostgreSQLデータベースを起動してください。rootにログインし、起動コマンドを実行します。
[e-fuse@albatross ~]$ su - パスワード: ←(表示されません) [root@albatross ~]# /etc/init.d/postgresql start データベースを初期化中: [ OK ] postgresql サービスを開始中: [ OK ] [root@albatross ~]#
最初に起動するときに、データベース領域を初期化するため上記のメッセージが出力されます。次回の起動からは「データベースを初期化中」というメッセージは出力されません。それでは、初期化されたデータベースを確認してみましょう。
- ターミナルより、
su - postgresを実行してpostgresユーザーにアクセスします psql -lコマンドを実行します
[root@albatross ~]# su - postgres
-bash-3.1$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
(3 rows)
-bash-3.1$
初期作成直後には、テンプレートデータベースである「template0」、それをコピーした「template1」、そして「postgres」データベースが作成されています。
2.データベースに接続する
既にコンソールで、postgresユーザーにアクセスしていると思いますが、postgresユーザーより初期作成されたデータベースに接続してみましょう。コマンドpsql postgresをターミナルより実行します。
-bash-3.1$ psql postgres
Welcome to psql 8.1.9, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
postgres=#
続いて、データベースの中身を確認してみましょう。psqlクライアントより、\dコマンドを実行します。
postgres=# \d No relations found. postgres=#
初期化直後は、データベースの中身は何も入っていないので「No relations found.」と表示されます。
4.サンプルテーブルを作成し表示する
接続しているデータベースにサンプルテーブルを作成し、データを投入してみましょう。ここでは、テーブル名「sampletable」とし、idnumber(serial型)、name(text型)の2項目で定義します。また、定義と同時にデータを数件投入してみます。
まず、createコマンドを実行します。今回は複数行に分けて入力しましたが、1行で入力することもできます。もし、文法的に不都合がある場合は、エラーの場所を指摘されるので、貼り付けミスがないか確認してください。
postgres=# CREATE TABLE sampletable postgres-# ( postgres(# idnumber serial, postgres(# name text postgres(# ) postgres-# ; NOTICE: CREATE TABLE will create implicit sequence"sampletable_idnumber_seq" for serial column "sampletable.idnumber" CREATE TABLE postgres=#
今回はserial型の項目を定義したので、シーケンスが自動的に定義されました。データベースの中身を確認してみましょう。\dコマンドを実行してください。
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------------------------+----------+----------
public | sampletable | table | postgres
public | sampletable_idnumber_seq | sequence | postgres
(2 rows)
postgres=#
テーブルと、シーケンスが作成されていることが分かります。
続いて、テーブルの中身を確認してみましょう。\d テーブル名を実行してください(今回はsampletableですので、\d sampletableとします)。
postgres=# \d sampletable
Table "public.sampletable"
Column | Type | Modifiers
----------+---------+-------------------------------------------------
idnumber | integer | not null default nextval
| | ('sampletable_idnumber_seq'::regclass)
name | text |
postgres=#
定義された項目と属性が表示されていることが分かります。
続いて、データを入力してみます。idnumberはシリアルで入力されるので、name項目だけ入力します。
postgres=# insert into sampletable (name) values ('fusekako');
INSERT 0 1
postgres=#
select文で入力されたデータを確認してみましょう。
postgres=# select * from sampletable;
idnumber | name
----------+----------
1 | fusekako
(1 row)
postgres=#
1件のデータが挿入されていることが分かりました。
5.データベースサービスを終了する
ターミナルよりサービスを終了します。rootにログインし、起動コマンドを実行します。
[e-fuse@albatross ~]$ su - パスワード: ←(表示されません) [root@albatross ~]# /etc/init.d/postgresql stop postgresql サービスを停止中: [ OK ] [root@albatross ~]#
以上でPostgreSQLデータベースサーバは終了しました。

"sampletable_idnumber_seq" for serial column "sampletable.idnumber"
CREATE TABLE
postgres=#