【IT168 文档】1. 假设环境
pgsql.home.net 192.168.1.3 255.255.255.0
文中的 port 选项是本人喜好,不是预设值或建议值
2. 更新 FreeBSD 中的 port tree
$ portsnap fetch update
$ exit
如果你从未用过 portsnap,改为
$ portsnap fetch extract update
$ exit
3. 登入 pgsql.home.net 后从 port 安装 perl 5.10.1
系统默认用 perl 5.8.x,如果你想 postgres 用 perl 5.10.x,有两个方法
i) 编译 postgresql 之前先编译 perl 5.10
ii) 在 /etc/make.conf 加入一行 PERL_VERSION=5.10.1
我习惯先编译 perl 5.10
$ cd /usr/ports/lang/perl5.10/
$ make config
┌─────────────────────────────┐
│ Options for perl-threaded 5.10.1 │
│ ──────────────────────────┐ │
│ │ [ ] DEBUGGING Build with debugging support │ │
│ │ [ ] GDBM Build GDBM_File extension │ │
│ │ [ ] PERL_MALLOC Use Perl malloc │ │
│ │ [X] PERL_64BITINT Use 64 bit integers (on i386) │ │
│ │ [X] THREADS Build threaded perl │ │
│ │ [ ] MULTIPLICITY Use multiplicity │ │
│ │ [X] SUIDPERL Build set-user-id suidperl binary │ │
│ │ [X] SITECUSTOMIZE Run-time customization of @INC │ │
│ │ [X] USE_PERL Rewrite links in /usr/bin │ │
$ make
$ make install
$ exit
4. 在 pgsql.home.net 从 port 安装 postgreSQL 8.4.1
$ cd /usr/ports/database/postgres84-server
$ make config
┌─────────────────────┐
│ Options for postgresql-server 8.4.1 │
│ ┌────────────────────────┐ │
│ │[X] NLS Use internationalized messages │ │
│ │[ ] PAM Build with PAM support (server only) │ │
│ │[ ] LDAP Build with LDAP authentication support │ │
│ │[ ] MIT_KRB5 Build with MIT's kerberos support │ │
│ │[ ] HEIMDAL_KRB5 Builds with Heimdal kerberos support │ │
│ │[ ] OPTIMIZED_CFLAGS Builds with compiler optimizations (-O3) │ │
│ │[X] XML Build with XML data type (server) │ │
│ │[X] TZDATA Use internal timezone database (server) │ │
│ │[ ] DEBUG Builds with debugging symbols │ │
│ │[X] INTDATE Builds with 64-bit date/time type (server)│ │
│ │ │ │
$ make
$ make install
$ exit
当编译 perl5.10 或 postgres 时,会自动跳进其他套件选项画面,
如 libiconv, m4, libxslt
我个人喜好的选择如下:
│ Options for libiconv 1.13.1 │
│ ┌────────────────────────┐ │
│ │ [X] EXTRA_ENCODINGS Include extra character sets │ │
│ │ [X] EXTRA_PATCHES Apply patches to fix CP932 add EUCJP-MS │ │
┌─────────────────────────┐
│ Options for m4 1.4.13,1 │
│ ┌───────────────────────────┐ │
│ │ [X] LIBSIGSEGV Use libsigsegv for better diagnostics │ │
┌─────────────────────────┐
│ Options for libxslt 1.1.26 │
│ ┌────────────────────────────┐ │
│ │ [X] MEM_DEBUG Enable memory debugging │ │
│ │ [X] CRYPTO Enable crypto support for exslt
安装 postgresql-contrib
$ cd /usr/ports/databases/postgresql-contrib
$ make
$ make install
$ exit
安装 p5-postgresql-plperl
$ su -
$ cd /usr/ports/databases/p5-postgresql-plperl
$ make
$ make install
$ exit
4. 在 pgsql.home.net 修改 postgreSQL Database 预设为中文 locate
本人使用繁体中文,选用 zh_TW.UTF-8 的 locate。请参考 postgresql 安装说明,它有提及如何改变 locate 的方法
$ pkg_info -D postgresql-server-8.4.1
我使用增加一个 login class 的方法,去改变 locate 值,步骤为
i) 修改 /etc/login.conf
ii) 修改 /etc/rc.conf
i)
$ vi /etc/login.conf
$ exit
在档案中某空白地方增加以下描述
# postgresql class
#-------------------
pgsql:\
:lang=zh_TW.UTF-8:
:setenv=LC_COLLATE=C:\
:tc=default:
修改完 /etc/login.conf 必顺执行一次 cap_mkdb 使之生效
$ cap_mkdb /etc/login.conf
ii)
$ vi /etc/rc.conf
$ exit
在档案中某空白地方增加以下描述
# postgresql settings
#----------------------
postgresql_enable="YES"
postgresql_data="/usr/local/pgsql/data"
postgresql_flags="-w -s -m fast"
postgresql_initdb_flags="--encoding=utf-8 --lc-collate=C"
postgresql_class="pgsql"
注意 /etc/rc.conf 中的 postgresql_class 值是对应 /etc/login.conf 名称
4. 在 pgsql.home.net 初始化 postgres 系统资料库
$ /usr/local/etc/rc.d/postgresql initdb
$ exit
5. 初始化系统资料库后,便可以第一次启动 postgresql
$ /usr/local/etc/rc.d/postgresql start
$ exit
6. 使用 psql 修改 pgsql 的密码
在 FreeBSD port 的 postgresql,它的 superuser 叫 pgsql,并不是 postgres
初始化的资料库,pgsql 点认值是没有密码,在 localhost 没有密码登入,任何人皆可以
用 pgsql 登入 postgresql 为所欲为。因此,第一件事为 pgsql 设密码,使用 psql
便可 $ psql -U pgsql postgres
在 psql 画面输入命令修改密码然后离开 psql
postgres=#\q
修改密码工作还有下半部份,便是修改 /usr/local/pgsql/data/pg_hba.conf
$ cd /usr/local/pgsql/data
$ vi pg_hba.conf
$ exit
把档案中的 trust 改为 md5,强迫 postgres 必须做密码登入
# "local" is for Unix domain socket connections only
local all pgsql md5
# IPv4 local connections:
host all pgsql 127.0.0.1/32 md5
host all pgsql 192.168.1.3/32 md5
host all all 192.168.1.0/24 reject
# IPv6 local connections:
#host all all ::1/128 trust
仔细的 pg_hba.conf 权限语法请自行参考资料
修改了 pgsql 的密码 和 pg_hba.conf 设定后,必须重新启动 postgresql
$ /usr/local/etc/rc.d/postgresql restart
$ exit
成功了,现在登入 postgresql 必须要输入密码
6. FreeBSD postgresql periodic script
FreeBSD port 会安装了一个 periodic 在 /usr/local/etc/periodic/daily/502.pgsql
这个 periodic script 提供两个服务:
i) 每天定时做 vacuumdb 一次
ii) 每天定时做 pg_dump 一次
想启动这功能 要修改 /etc/periodic.conf
或 /etc/periodic.conf.local 二者选一,内容一样
$vi /etc/periodic.conf
$ exit
档案中增加以下内容
daily_pgsql_backup_enable="YES" # do backup
daily_pgsql_vacuum_enable="YES" # do vacuum
如果你只想做 vacuum 不做 pg_dump,内容如下
档案中增加以下内容
daily_pgsql_backup_enable="NO" # do backup
daily_pgsql_vacuum_enable="YES" # do vacuum
由于 postgresql 已改变为加密登入,使用 periodic script 必需提供 password 给
periodic,否则会执行 vacuumdb pg_dump,失败。方法是建立一个密码 .pgpass
档案把 password 传给 periodic
.pgpass 档案必需放置在 postgresql daemon 的 $HOME 才可以配合 periodic 顺利执行
检察 postgresql 的 $HOME 在那里
$more /etc/passwd | grep PostgreSQL
$ exit
我的查询结果如下
pgsql:*:70:70::0:0:PostgreSQL Daemon:/usr/local/pgsql:/bin/sh
表示 postgresql 的 $HOME 在 /usr/local/pgsql 这里
因此把 .pgpass 放置在 /usr/local/pgsql 成为
/usr/local/pgsql/.pgpass
步骤如下:
$ cd /usr/local/pgsql
$ touch .pgpass
$ chmod 600 .pgpass
$ chown pgsql:pgsql .pgpass
$ exit
修改 .pgpass 内容
$ vi /usr/local/pgsql/.pgpass
$ exit
.pgpass 格式有规范的,官方说明请看:http://www.postgresql.org/docs/8.4/interactive/libpq-pgpass.html
localhost:5432:*:pgsql:你的pgsql密码
最后一步,是修改 /usr/local/etc/periodic/daily/502.pgsql 使它懂得配对
正确的用户名和密码
$ vi /usr/local/etc/periodic/daily/502.pgsql
$ exit
把其中的两行参数修改
< 原本是 >
daily_pgsql_pgdump_args="-b -F c"
< 修改为 >
daily_pgsql_pgdump_args="-h localhost -U pgsql -b -F c"
基本安装完成。