UJP - 技術情報1

Life is fun and easy!

不正IP報告数

Okan Sensor
 
メイン
ログイン
ブログ カテゴリ一覧

PostgreSQLで初期データベースを作成する

PostgreSQLで初期データベースを作成する


0.改訂履歴

  • 2005.09.29 新規作成

1.はじめに

 このドキュメントでは,PostgreSQLの初期データベースの作成手順を説明する. データベースをインストールした後に,この初期データベースの作成をinitdbコマンドを使って行う必要があるが,この作業はサーバ1台につき1回だけでよい. また,データベースデータを格納するためのディレクトリ,ログファイルの定義についても説明している.

2.インストール環境の確認

  • 現在のユーザIDを確認する.
[postgres@mars postgres]$ id
uid=501(postgres) gid=503(postgres) groups=503(postgres)
[postgres@mars postgres]$  
  • Postgresのツールディレクトリで,何が提供されているか確認する.
[postgres@mars postgres]$ cd /usr/local/pgsql/bin/
[postgres@mars bin]$ ls -la
total 3356
drwxr-xr-x    2 postgres postgres     4096 Sep  9 15:26 .
drwxr-xr-x    8 postgres postgres     4096 Sep  9 15:26 ..
-rwxr-xr-x    1 postgres postgres    24546 Sep  9 15:26 clusterdb
-rwxr-xr-x    1 postgres postgres    24525 Sep  9 15:26 createdb
-rwxr-xr-x    1 postgres postgres    28623 Sep  9 15:26 createlang
-rwxr-xr-x    1 postgres postgres    24956 Sep  9 15:26 createuser
-rwxr-xr-x    1 postgres postgres    22961 Sep  9 15:26 dropdb
-rwxr-xr-x    1 postgres postgres    27992 Sep  9 15:26 droplang
-rwxr-xr-x    1 postgres postgres    23027 Sep  9 15:26 dropuser
-rwxr-xr-x    1 postgres postgres   348760 Sep  9 15:26 ecpg
-rwxr-xr-x    1 postgres postgres    38288 Sep  9 15:26 initdb
-rwxr-xr-x    1 postgres postgres     4777 Sep  9 15:26 initlocation
-rwxr-xr-x    1 postgres postgres     3013 Sep  9 15:26 ipcclean
-rwxr-xr-x    1 postgres postgres     2328 Sep  9 15:26 pg_config
-rwxr-xr-x    1 postgres postgres    12669 Sep  9 15:26 pg_controldata
-rwxr-xr-x    1 postgres postgres    10226 Sep  9 15:26 pg_ctl
-rwxr-xr-x    1 postgres postgres   159035 Sep  9 15:26 pg_dump
-rwxr-xr-x    1 postgres postgres    30549 Sep  9 15:26 pg_dumpall
-rwxr-xr-x    1 postgres postgres     6569 Sep  9 15:26 pg_encoding
-rwxr-xr-x    1 postgres postgres     6682 Sep  9 15:26 pg_id
-rwxr-xr-x    1 postgres postgres    18398 Sep  9 15:26 pg_resetxlog
-rwxr-xr-x    1 postgres postgres    80379 Sep  9 15:26 pg_restore
-rwxr-xr-x    1 postgres postgres  2305090 Sep  9 15:26 postgres
lrwxrwxrwx    1 postgres postgres        8 Sep  9 15:26 postmaster -> postgres
-rwxr-xr-x    1 postgres postgres   149054 Sep  9 15:26 psql
-rwxr-xr-x    1 postgres postgres    14780 Sep  9 15:26 vacuumdb
[postgres@mars bin]$
  • ファイル名をみただけはわからないが,インストール後のディレクトリ構成がどうなっているか確認する.
[postgres@mars bin]$ find /usr/local/pgsql/ -name "*" -type d -print
/usr/local/pgsql/
/usr/local/pgsql/doc
/usr/local/pgsql/doc/html
/usr/local/pgsql/man
/usr/local/pgsql/man/man1
/usr/local/pgsql/man/man7
/usr/local/pgsql/bin
/usr/local/pgsql/share
/usr/local/pgsql/lib
/usr/local/pgsql/include
/usr/local/pgsql/include/libpq
/usr/local/pgsql/include/internal
/usr/local/pgsql/include/internal/libpq
/usr/local/pgsql/include/internal/lib
/usr/local/pgsql/include/informix
/usr/local/pgsql/include/informix/esql
[postgres@mars bin]$

3.データベースを作成する

  • 作成したデータベースファイルを格納するディレクトリを作成する.
[postgres@mars bin]$ mkdir /usr/local/pgsql/database
[postgres@mars bin]$
  • データベースを作成するプログラムを探す.
[postgres@mars postgres]$ locate initdb|grep bin
/usr/local/pgsql/bin/initdb
[postgres@mars postgres]$
  • このコマンドを使ってデータベースを作成する.
  • 日本語を利用する場合は,現在のバージョンでは無条件にEUC_JPを指定する.
  • それがSJISを使いたいという時でも!
    • データベース内部にどのような形式で保存されていてもクライアントから指定した文字コードでトランスレータによって変換されるので,問題ない.
  • データベースを格納するディレクトリと文字コードを指定して,次のように実行する.
[postgres@mars postgres]$ /usr/local/pgsql/bin/initdb ¥
> -D /usr/local/pgsql/database ¥
> --encoding=EUC_JP
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale C.

creating directory /usr/local/pgsql/database... ok
creating directory /usr/local/pgsql/database/base... ok
creating directory /usr/local/pgsql/database/global... ok
creating directory /usr/local/pgsql/database/pg_xlog... ok
creating directory /usr/local/pgsql/database/pg_clog... ok
selecting default max_connections... 100
selecting default shared_buffers... 1000
creating configuration files... ok
creating template1 database in /usr/local/pgsql/database/base/1... ok
initializing pg_shadow... ok
enabling unlimited row size for system tables... ok
initializing pg_depend... ok
creating system views... ok
loading pg_description... ok
creating conversions... ok
setting privileges on built-in objects... ok
creating information schema... ok
vacuuming database template1... ok
copying template1 to template0... ok

Success. You can now start the database server using:

    /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/database
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/database -l logfile start

[postgres@mars postgres]$  
  • データベースの起動用コマンドがかかれている.
  • 起動する前に,データベースが作成されたディレクトリを確認する.
[postgres@mars postgres]$ cd /usr/local/pgsql/database
[postgres@mars database]$ ls -la
total 44
drwx------    6 postgres postgres     4096 Sep 14 16:37 .
drwxr-xr-x   10 postgres postgres     4096 Sep 14 16:37 ..
-rw-------    1 postgres postgres        4 Sep 14 16:37 PG_VERSION
drwx------    4 postgres postgres     4096 Sep 14 16:37 base
drwx------    2 postgres postgres     4096 Sep 14 16:37 global
drwx------    2 postgres postgres     4096 Sep 14 16:37 pg_clog
-rw-------    1 postgres postgres     3288 Sep 14 16:37 pg_hba.conf
-rw-------    1 postgres postgres     1441 Sep 14 16:37 pg_ident.conf
drwx------    2 postgres postgres     4096 Sep 14 16:37 pg_xlog
-rw-------    1 postgres postgres     7781 Sep 14 16:37 postgresql.conf
[postgres@mars database]$ 
  • データベースのログ格納用ディレクトリを作成する.
[postgres@mars database]$ mkdir log
[postgres@mars database]$ cd log
[postgres@mars log]$ pwd
/usr/local/pgsql/database/log
[postgres@mars log]$ cd ..
[postgres@mars database]$  
  • このディレクトリには,データベース起動時に指定し,ここで格納されたファイルには,PostgreSQLの活動状況のログが格納される.


広告スペース
Google