Thursday, February 12, 2009

postgres学习之(1): postgres的安装

工作中需要使用到postgres, 为了方便, 决定学习一把postgres的安装与管理!(日后不怕事呀 ^_^)
* 文章内容全是个人摸索过程, 废话比较多. 应该少看为好 (^_^)

Step One:
当然是安装了, 在FreeBSD下, postgres的server Port的版本需要与postgres的client Port版本一一对应的, 就是说:
* postgres的client8.2就要与postgres的server8.2对应, 不过安装会出错. 例子如下:
本人的机器中早已经安装了postgres82-client. 当本人想去安装postgres83-server时就出错, 解决方法有二:
1. pkg_delete 8.2的client, 安装8.3的client
1. 安装8.2的server

本人选择了第二种, ^_^反正是学习, 方便就行了.

安装完server后, 就会增加一个系统用户名:pgsql
* 注意, 在linux系统下, 此用户名应该是postgres! 我是相当地 囧!!

Step Two:
check一把配置文件. FreeBSD下是/usr/local/pgsql, 不过, 现在还不知道从何入手.

小白的我只知道系统下管理postgres操作是使用系统的特殊用户: 那就是pgsql了!!

切换过去吧:
# su pgsql
$ bash
[pgsql@study /]$
* 这样就使用pgsql用户了. 这是有几个pgsql用户可以使用的命令:
pg_config: retrieve information about the installed version of Postgres
pg_ctl: start, stop, or restart a PostgreSQL server
pg_dumpall: extract a PostgreSQL database cluster into a script file
pg_restore: restore a PostgreSQL database from an archive file created by pg_dump
pg_controldata: display control information of a PostgreSQL database cluster
pg_dump: extract a PostgreSQL database into a script file or other archive file
pg_resetxlog: reset the write-ahead log and other control information of a PostgreSQL database cluster

逐一man一次! ^_^ 无敌的man呀~!~!~!~

发现man不是万能的, 还是看书吧!囧!!

在书上找到了一点灵感, 就是使用pkg_info


pkg_info -L postgresql-server-8.2.9|less

看到了一个initdb, man了一下, 看到cluster这个名词, 这不是pg_*系列命令中的一个有这个单词的吗?

哈, 就是pg_controldata了, 看来两者相关!

每个数据库都有一个叫data的数据目录的, 新安装的postgres还没有看到, 看到是使用initdb的时候了.

好晕, initdb一堆ooptions! 其实, initdb只有个option是必选的: --pgdata=directory

不理, 试一下简单的:

[pgsql@study /usr/ports/databases/postgresql82-server]$ initdb --pgdata=/usr/local/pgsql/
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.

The database cluster will be initialized with locales
COLLATE: C
CTYPE: zh_CN.UTF-8
MESSAGES: C
MONETARY: C
NUMERIC: C
TIME: C
The default database encoding has accordingly been set to UTF8.

initdb: directory "/usr/local/pgsql" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/pgsql" or run initdb
with an argument other than "/usr/local/pgsql".

* 发现不行, pgdata目录一定要为空的, 结果使用/usr/local/pgsql/data就OK了

成功initdb后, 有如下内容:

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

postgres -D /usr/local/pgsql/data
or
pg_ctl -D /usr/local/pgsql/data -l logfile start

* 从上面的info可以得到三个信息:
1. 配置文件为pg_hba.conf
1. postgres的启动方式
1. 现在这个cluster的认证方式
* 配置配置文件就不能猜了, 只能看手册了.


本人只修改两个文件:
* pg_hba.conf
* postgresql.conf

postgres服务正常启动! good boy

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.