PostgreSQL安装

0.编译环境

  • Linux: CentOS 5.5
  • gcc: 4.1.2

\1. 安装PostgreSQL

1) 解压postgresql-9.1.7.tar.bz2

#tar jxvf postgresql-9.1.7.tar.bz2

img

2) 进入解压后的postgresql-9.1.7目录

#cd postgresql-9.1.7

img

3) 编译postgresql源码

#./configure –prefix=/opt/pgsql-9.1.7

img

#make

img

#make install

img

至此,完成postgresql的安装。进入/opt/pgsql-9.1.7目录可以看到安装后的postgresql的文件。

#ls /opt/pgsql-9.1.7

img

2.创建postgresql数据库

1) 创建postgres用户

#useradd postgres

修改postgres密码

#passwd postgres

img

2) 设置postgres用户的环境变量

切换到postgres用户

#su - postgres

img

进入postgres的主目录

#cd ~

img

编辑~/.bash_profile文件

#vi ~/.bash_profile

设置以下的环境变量

export PGHOME=/opt/pgsql-9.1.7

export PGDATA=~/data

img

保存,退出vi。执行以下命令,使环境变量生效

#source ~/.bash_profile

img

3) 初始化postgres数据库

#initdb

img

至此,完成postgres数据库的初始化。

4) 启动postgres数据库实例

#pg_ctl start

img

可以看到postgresql数据库实例已经启动,通过下面的命令可以查看系统中运行的postgres进程

#ps -ef | grep postgres

img

5) 连接postgresql数据库

#psql -h 127.0.0.1 -d postgres -U postgres

img

6) 停止postgresql数据库实例

#pg_ctl stop

#ps -ef | grep postgres

img

可以看到已经没有postgres进程

\3. 设置PostgreSQL开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下

img

linux文件即为linux系统上的启动脚本

1)修改linux文件属性,添加X属性

#chmod a+x linux

2) 复制linux文件到/etc/init.d目录下,更名为postgresql

#cp linux /etc/init.d/postgresql

3)修改/etc/init.d/postgresql文件的两个变量

prefix设置为postgresql的安装路径:/opt/pgsql-9.1.2

PGDATA设置为postgresql的数据目录路径:

img

4) 执行service postgresql start,就可以启动PostgreSQL服务

#service postgresql start

img

5)设置postgresql服务开机自启动

#chkconfig –add postgresql

执行上面的命令,就可以实现postgresql服务的开机自启动。