Chinaunix首页 | 论坛 | 博客
  • 博客访问: 375706
  • 博文数量: 75
  • 博客积分: 1732
  • 博客等级: 上尉
  • 技术积分: 812
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-11 16:02
个人简介

博客很久没有更新了,原因是大多数时间都忙在研究技术上,却懒得腾时间出来把技术分享,最近在开源力量上开课《Mongodb管理与维护》,让屌丝们从0到精通,敬请关注。本博客技术原创更新滞后一些,找时间更新有关mysql,mongodb等内容,谢谢大家关注。

文章分类

全部博文(75)

文章存档

2021年(1)

2011年(20)

2010年(40)

2009年(7)

2008年(7)

分类: Mysql/postgreSQL

2010-11-17 11:12:25


Postgresql创建新实例过程详解


一、postgresql系统安装

#apt-get install # apt-get install postgresql-8.3

二、创建新实例(debian linux):

使用语法: /usr/bin/pg_createcluster [options]

# pg_createcluster -u postgres -d /export/wushen_game -e utf8 --start 8.3 wushen_game

Creating new cluster (configuration: /etc/postgresql/8.3/wushen_game, data: /export/wushen_game)...

Moving configuration file /export/wushen_game/postgresql.conf to /etc/postgresql/8.3/wushen_game...

Moving configuration file /export/wushen_game/pg_hba.conf to /etc/postgresql/8.3/wushen_game...

Moving configuration file /export/wushen_game/pg_ident.conf to /etc/postgresql/8.3/wushen_game...

Configuring postgresql.conf to use port 5434...

 

pg_createcluster -u postgres -d /export/wushen_game -e utf8 --start 8.3 wushen_game

涵义解释:

1.       –u指明新建数据库实例所有者为postgres,

2.       –d指明数据库实例目录在/export/wushen_game下,如果该目录不存在创建它。

3.       –e指明新建数据库方案时默认字符编码为utf8

4.       –start放在系统/etc/init.d/postgresql一起管理,创建完成后即启动。

5.       8.3指明实例兼容的版本号

6.       wushen_game指定新实例的名字,新建实例后,可以通过pg_lsclusters来查看新创建的实例详细,如下图实例名wushen_game版本号,集群名,端口,是否已经启动,数据目录,日志文件路径等。

# pg_lsclusters

Version Cluster   Port Status Owner    Data directory                     Log file

8.3     pay       5433 online postgres /var/lib/postgresql/8.3/pay        /var/log/postgresql/postgresql-8.3-pay.log

8.3     test0612  5432 online postgres /var/lib/postgresql/8.3/test0612   /var/log/postgresql/postgresql-8.3-test0612.log

8.3     wushen_game 5434 online postgres /export/wushen_game                /var/log/postgresql/postgresql-8.3-wushen_game.log

 

三、创建新实例内部过程详解

创建过程使用pg_createcluster,我们来看一下这个脚本

# whereis pg_createcluster

pg_createcluster: /usr/bin/pg_createcluster /usr/share/man/man8/pg_createcluster.8.gz

# vi /usr/bin/pg_createcluster 这脚本为perl编写的创建和初始化postgresql实例的过程

其中创建过程主要调用的函数顺序如下:

(1).调用get_encoding_from_locale 获取linux本地编码环境列表,如果本地环境编码都不可用,那么postgresql配置文件中将默认使用SQL_ASCII编码.

(2). 调用init_db初始化数据库:

第一步:检查是否指定了实例目录,创建时是否以root用户创建(必须是root权限)。如果检查权限不是root,将中断报错“could not create data directory; you might need to run this program with root privileges”。

第二步:如果第一步检查通过,那么将获取postgresqlinitdb命令行参数选项和系统字符集参数,如果参数没有错误,将执行initdb函数初始化。如果成功,那么将向终端输出:

Creating new cluster (configuration: /etc/postgresql/8.3/wushen_game, data: /export/wushen_game)...

第三步:根据命令行参数输入的版本号,实例名生成配置文件。如果这一步成功,那么将向终端输出

Moving configuration file /export/wushen_game/postgresql.conf to /etc/postgresql/8.3/wushen_game...


3)调用 setup_pg_hba函数

     如果成功,那么将向终端输出一行:

     Moving configuration file /export/wushen_game/pg_hba.conf to /etc/postgresql/8.3/wushen_game...

    执行其它判断生成默认pg_ident.conf文件并向终端输出:

Moving configuration file /export/wushen_game/pg_ident.conf to /etc/postgresql/8.3/wushen_game...

4)。如果以上过程都执行成功,那么将调用系统命令

              /usr/bin/pg_ctlcluster 8.3 wushen_game start 启动新建实例。

      最后通过pg_lsclusters来查看新实例是否启动;

 

四、实例启动、停止和重启

1、  多实例启动、停止和重启:# /etc/init.d/postgresql-8.3 start|stop|restart

例如执行# /etc/init.d/postgresql-8.3 stop后,三个实例都停止了,如下。

# /etc/init.d/postgresql-8.3 stop

Stopping PostgreSQL 8.3 database server: pay test0612 wushen_game.

# pg_lsclusters

Version Cluster   Port Status Owner    Data directory                     Log file

8.3     pay       5433 down   postgres /var/lib/postgresql/8.3/pay        /var/log/postgresql/postgresql-8.3-pay.log

8.3     test0612  5432 down   postgres /var/lib/postgresql/8.3/test0612   /var/log/postgresql/postgresql-8.3-test0612.log

8.3     wushen_game 5434 down   postgres /export/wushen_game                /var/log/postgresql/postgresql-8.3-wushen_game.log

例如执行# /etc/init.d/postgresql-8.3 start后,三个实例都启动,如下。

# /etc/init.d/postgresql-8.3 start

Starting PostgreSQL 8.3 database server: pay test0612 wushen_game.

# pg_lsclusters

Version Cluster   Port Status Owner    Data directory                     Log file

8.3     pay       5433 online postgres /var/lib/postgresql/8.3/pay        /var/log/postgresql/postgresql-8.3-pay.log

8.3     test0612  5432 online postgres /var/lib/postgresql/8.3/test0612   /var/log/postgresql/postgresql-8.3-test0612.log

8.3     wushen_game 5434 online postgres /export/wushen_game                /var/log/postgresql/postgresql-8.3-wushen_game.log

2、单实例启动、停止和重启:# /usr/bin/pg_ctlcluster 8.3 wushen_game start|stop|restart

例如执行# /usr/bin/pg_ctlcluster 8.3 wushen_game stop后,只有wushen_game实例停止了。

# /usr/bin/pg_ctlcluster 8.3 wushen_game stop

# pg_lsclusters

Version Cluster   Port Status Owner    Data directory                     Log file

8.3     pay       5433 online postgres /var/lib/postgresql/8.3/pay        /var/log/postgresql/postgresql-8.3-pay.log

8.3     test0612  5432 online postgres /var/lib/postgresql/8.3/test0612   /var/log/postgresql/postgresql-8.3-test0612.log

8.3     wushen_game 5434 down   postgres /export/wushen_game                /var/log/postgresql/postgresql-8.3-wushen_game.log

 

 

阅读(5979) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-17 16:41:07

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com