PostgreSQL に関するインストールメモ(2002/02/24) 
 

PostgreSQLを動かす環境

・Postgres-7.2 + Perl 5.6.1 + メモリ多数

PostgreSQLとは

Qube3J の記事を参照にしてください。

本家は、ここ   : PostgreSQL-FAQ

商用データベースに負けないPostgreSQL 「第1回第2回最終回

PostgreSQL

 

設定方法

1.postgres グループ/アカウントの作成を行う。

# groupadd -g 1100 users
# useradd -u 1100 -g users -d /export/home/postgres -s /bin/tcsh -m postgres
# passwd postgres

2.postgres のユーザ情報を定義(~/.cshrc)する。

setenv postgresdir /opt/local/pgsql
setenv PGLIB $postgresdir/lib
setenv PGDATA /export/home/pgsql_data
set path=( /usr/ccs/bin /usr/local/bin /usr/local/sbin \
               /opt/sfw/bin /usr/bin /bin /usr/sbin /sbin \
              /usr/openwin/bin /usr/ucb $postgresdir/bin)
setenv MANPATH /usr/local/man:/usr/share/man:/usr/man:$postgresdir/man
setenv LD_LIBRARY_PATH /usr/local/lib:/lib:/usr/lib:$PGLIB

3.インストールを行う。ここでは postgres と、Perl からのアクセスモジュール Pg をインストールする設定を説明する。

% cd /opt/local/src
% wget ftp://ftp.sra.co.jp/pub/cmd/postgres/7.2/postgresql-7.2.tar.gz
% gzip -cd postgresql-7.2.tar.gz | tar xf -
% cd postgresql-7.2
% env CFLAGS=-O3 LDFLAGS="-L/opt/sfw/lib -R/opt/sfw/lib" ./configure --enable-multibyte=EUC_JP --prefix=/opt/local/pgsql --with-perl
% su
# make ← 結構時間かかります
All of PostgreSQL successfully made. Ready to install. と表示されたら問題なし
# make install
# cd ./src/interfaces/perl5
# perl Makefile.PL
# make clean ; make
# make install

4.データベース領域を確保する。

# mkdir -p /export/home/postgres
# chown -R postgres:users /export/home/postgres

5.データベースの初期化を行う。

postgres ユーザで行う
% initdb
% /opt/local/pgsql/bin/pg_ctl -w start

*.データベースの初期化時(pg_ctl -w start)に、以下のメッセージが表示される場合があります。これは、システムで使用するメモリ・キャッシュ・・・等々の値が小さいためです。改善策として /etc/system にその情報を増やす様に記述しなおします(要再起動)。

エラー内容

waiting for postmaster to start....IpcMemoryCreate: shmget(key=5432001, size=1417216, 03600) failed: Invalid argument

This error usually means that PostgreSQL's request for a shared memory
segment exceeded your kernel's SHMMAX parameter. You can either
reduce the request size or reconfigure the kernel with larger SHMMAX.
To reduce the request size (currently 1417216 bytes), reduce
PostgreSQL's shared_buffers parameter (currently 64) and/or
its max_connections parameter (currently 32).

If the request size is already small, it's possible that it is less than
your kernel's SHMMIN parameter, in which case raising the request size or
reconfiguring SHMMIN is called for.

The PostgreSQL Administrator's Guide contains more information about
shared memory configuration.

改善策(/etc/system)ファイルを下記の様に編集する。

set shmsys:shminfo_shmmax=4294967295
set shmsys:shminfo_shmmin=1
set shmsys:shminfo_shmmni=100
set shmsys:shminfo_shmseg=10
set semsys:seminfo_semmni=100
set semsys:seminfo_semmsl=100
set semsys:seminfo_semmns=200
set semsys:seminfo_semopm=100
set semsys:seminfo_semvmx=32767

データベースの作成

テスト用のデータベースを作成します。

postgres% createdb test

サンプルテーブルを作成する

サンプルテーブルを作成しておきましょう。

test=> create table t2 (code integer, name text);
CREATE
test=> insert into t2(code,name) values(111,'hoge');
INSERT 19018 1
hogedb=> select * from t2;
 code | name
------+------
 111  | hoge
(1 row)

利用者情報の登録

一般ユーザが Postgres のアクセスツールである psql をそのまま使おうとすると以下のエラーメッセージが表示されます。

hoge% psql test
psql: FATAL 1: SetUserId: user 'hoge' is not in 'pg_shadow'

Postgres の利用者ユーザとして hoge ユーザを登録します。

postgres% createuser hoge
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

ついでに hoge ユーザをデータベースアクセスできるように登録します。

postgres% psql test
hogedb=# grant all on test.* to hoge
hogedb=# \q

hoge ユーザを登録後、再度 psql を実行します。

hoge% psql test
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

test=>

自動起動の設定方法

1.自動起動できるように設定します。postgres の起動スクリプトは、ここ

2.init.d および rc3.d の設定を行います。

# vi /etc/init.d/postgres           ← これを編集する
# chmod 755 /etc/init.d/postgres
# ln -s /etc/init.d/postgres /etc/rc0.d/K10postgres
# ln -s /etc/init.d/postgres /etc/rc2.d/S99postgres