Bon Long Ray - Documents for our current project.

于 Xubuntu (Drapper) 安装 PostgreSQL 8.1

1 安装及配置

1. 1 安装

安装源中的 pg8.1(5个包)及pgadmin3(2个包)。

安装中提示load gnome错误,缺libgnome2_perl,无法创建图形界面。

配好后重装,没有出现过图形界面,也没有此前出现过得"load gnome"过程。

经落实,确实可弹出图形界面,不过,图形界面仅仅是一个许可声明,可不装这个lib

1.2 修改postgres系统帐户的密码

首先,打开Xubuntu的"用户和组" ,在"用户" 标签页下,勾选"所有用户"选框,能够看到 "postgres",PG安装过程中自动创建的超级用户。

打开属性窗口后,手动修改密码。

此时,已经可以使用
List 1.2.1:
$ su postgres
Password:
postgres@... $ psql

进入数据库的字符终端。

1.3 修改postgres帐户于pg中的密码

此时,由于仍不知道数据库中postgres的密码,所以无法使用pgadmin3连接到数据库,需要使用postgres用户修改进入psql修改密码。

按照List 1.2.1的方式进入psql中后,
List 1.3.1:
postgres=# ALTER USER postgres WITH PASSWORD 'your-password';
ALTER ROLE

完成这一步骤后,仍不能用pgadmin3连到数据库,需要下面的修改。

1.4 修改服务器监听IP范围

在 /etc/postgresql/8.1/main/postgresql.conf 中,找到 listen_addresses = 'localhost',将其前面的注释符取消。

或者,可以指定特定的网段。

至此,已经可以使用pgadmin3连接到数据库。

1.5 需要在 /etc/profile 中添加如下内容以确保如pg_ctl等工具的路径可视:

$ export PATH=/usr/lib/postgresql/8.1/bin:$PATH   

2 基本操作

2.1 启动数据库服务器

首先检查服务器状态:
List 2.1.1:
postgres@...$ pg_lsclusters   
Version Cluster   Port Status Owner    Data directory                     Log file
8.1     main      5432 down   postgres /var/lib/postgresql/8.1/main       /var/log/postgresql/postgresql-8.1-main.log

确认如上状态后,启动服务器:
List 2.1.2:
postgres@...$pg_ctlcluster 8.1 main start

此时,检查服务器状态如下:
List 2.1.3:
Version Cluster   Port Status Owner    Data directory                     Log file
8.1     main      5432 online postgres /var/lib/postgresql/8.1/main       /var/log/postgresql/postgresql-8.1-main.log

2.2 关闭服务器

如上,命令为
List 2.2.1:
postgres@...$ pg_ctl -D /etc/postgresql/8.1/main/pgdata stop
等待 postmaster 关闭 .... 完成

关键是要定位postmaster.pid,所谓的pg_ctlcluster无效(见 List 2.2.2),就是因为路径问题,在 stop和restart时,无法定位到 $info 'pgdata'(见 List 2.2.3) 。
List 2.2.2:
postgres@... $ pg_ctlcluster 8.1 main stop
Insecure directory in $ENV{PATH} while running with -T switch at /usr/bin/pg_ctlcluster line 342.
Insecure directory in $ENV{PATH} while running with -T switch at /usr/bin/pg_ctlcluster line 350. 
(does not shutdown gracefully, now stopping immediately
List 2.2.3:
 sub stop {
    autovacuum_stop if $pg_autovacuum;
     stop_check_pid_file;
     if (!fork()) {
        close STDOUT;
        exec $pg_ctl, '-D', $info{'pgdata'}, '-s', '-w', '-m', 'fast', 'stop';
     } else {
        wait;
    }
     # try harder if "fast" mode does not work
    if (-f $info{'pgdata'}.'/postmaster.pid') {
        print "(does not shutdown gracefully, now stopping immediately)"; 
        system $pg_ctl, '-D', $info{'pgdata'}, '-s', '-w', '-m', 'immediate', 'stop';
    }
     # if that still not helps, use the big hammer
    if (-f $info{'pgdata'}.'/postmaster.pid') {
        print "(does not shutdown, killing the process)"; 
        if (open FPID, $info{'pgdata'}.'/postmaster.pid') {
            $pid = <FPID>;
            close FPID;
        }
        kill (9, $pid) if $pid;
        unlink $info{'pgdata'}.'/postmaster.pid'; 
    }
}


但很奇怪的是,在 start 时却可以准确定位。

将 /etc/postgresql/8.1/main/pg_ident.conf|postgresql.conf 复制到 postgres 用户主目录下的 8.1/main/。必要时,需要删除 postmaster.pid。而后,通过
pg_ctl start -D /etc/postgresql/8.1/main/pgdata/ -l log
启动数据库服务器,通过
pg_ctl stop -D /etc/postgresql/8.1/main/pgdata/
终止。

Someday: 配置pg的环境变量,解决 pg_cluster 的问题。
________
(完)

没有评论: