T:freebsd:pgsql

出典: Tariki

目次

PostgreSQLのインストールと稼動

インストール

インストール自体はports/packagesにお任せ。

→pgsqlなるuid/gidができる。

(データディレクトリがない場合)

# cd /usr/local/pgsql/
# mkdir data
# chown pgsql:pgsql data
# su pgsql ←アカウントはshが使えるよう。
$ initdb -D ./data --encoding=utf-8 --lc-collate=C

/etc/rc.confを編集

postgresql_enable="YES"
postgresql_flags="-w -s -m fast"
postgresql_initdb_flags="--encoding=utf-8 --lc-collate=C"
postgresql_class="default"

起動。

# /usr/local/etc/rc.d/010.pgsql start

ユーザの作成とパーミッション

PHP on Apache2.xから駆動することを前提。生いじりするときはアカウントtarohさんでもいじれるよう(pgsqlスーパーユーザ)にする。

# su pgsql
$ createuser nobody
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE
$ createuser taroh
Shall the new role be a superuser? (y/n) y
CREATE ROLE ←superuserについては権限の質問がない。

DBの作成とパーミッション

以降の作業はpgsqlスーパーユーザ(pgsqlさんまたは上記例ならtarohさん)で。

> createdb hoge
CREATE DATABASE
> psql hoge
hoge=# CREATE TABLE moge (
 nantoka text primary key,
 kantoka text
);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "moge_pkey" for table "moge"
↑なんか言われるが気にしない。
テキストがprimary keyでは都合が悪いので内部処理上のprimary keyが追加されたと解釈。
CREATE TABLE
hoge=# grant all on moge to nobody;  ←これやっとかないとPHP on Apacheからアクセスできない
GRANT

pgsql DBが壊れた

[6-1] could not locate a valid checkpoint record

transaction logが狂ったみたいです。

# su pgsql
$ pg_resetxlog /usr/local/pgsql/data

ちなみに原因はHDDの過熱→RAIDボックス取替え→fsckで大量のデータ不整合発覚 (2010.3)。