1、登陆Centos使用root用户登陆
下载postgresql
#:wget http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.gz
解压:
#tar zxvf postgresql-9.2.4.tar.gz
安装编译postgresql需要的编译工具(在root根目录执行,根目录:root登录进入的目录)
yum install gcc cpp glibc glibc-devel gcc-c++ make
进入解压后的目录
#cd postgresql-9.2.4
开始编译
#./configure
-------------------------编译遇到的问题以及解决方案----------------------------------------
First:如果编译出现错误:
configure:error: in'/root/postgresql-9.2.4':
configure:error: no acceptable C compiler foundin $PATH
原因是没有GCC安装即可(在root根目录执行)
#yum -y install gcc
Second:如果编译出现错误:
configure: error: readline library not found
原因是缺少readline库,安装即可(在root根目录执行)
#yum -y install readline-devel
Third:如果编译出现错误:
configure: error: zlib library not found
安装zlib(在root根目录执行)
#yum -y install zlib-devel
执行后末尾不在显示configure:error说明顺利通过
#gmake
#gmake install
到这里就完成安装,接下来就是配置了
2、环境变量(这里使用的是vim编辑器,也可选用其他编辑器可供使用,看个人爱好选择)
在root根目录执行下面的vim操作:
#vim .bash_profile
把:PATH=$PATH:$HOME/bin
改为:PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin
保存退出,
让环境变量生效:
#. .bash_profile
3、建立数据库用户
#useradd postgres
修改用户密码(root用户可以直接使用passwd命令,不用加用户名)
#passwd postgres
输入两次新的密码:postgres(和用户名相同)
更改用户目录(这一步可选)
#vim /etc/passwd
把
postgres:x:500:500::/home/postgres:/bin/bash
改为
postgres:x:500:500::/usr/local/pgsql:/bin/bash
删除用户目录:
rm -r /home/postgres
4、初始化数据库
新建数据目录:
#mkdir /usr/local/pgsql/data
更改权限:
#chown postgres:postgres /usr/local/pgsql/data
切换到刚刚建立的用户
#su postgres
$cd /usr/local/pgsql
$initdb -D data/
到这里数据的初始化就完成了。
5、接下来是系统服务了:
回到root用户:
$exit
复制安装目录下的linux文件到/etc/init.d/
进入postgresql的安装目录,(即刚刚使用tar命令解压的目录)
#cd /root/postgresql-9.2.4
#cp contrib/start-scripts/linux /etc/init.d/postgresql
添加执行权限:
#chmod +x /etc/init.d/postgresql
启动数据库服务
#service postgresql start
Starting PostgreSQL: ok 大功告成了。
停止数据库服务
service postgresql stop
Stoping PostgreSQL: ok 停止成功。
别激动还有一步呢,让数据库开机启动,
#chkconfig --add postgresql
#chkconfig postgresql on
等等好像还少一个东西:
数据库操作的历史记录文件
#touch /usr/local/pgsql/.psql_history
#chown postgres:postgres /usr/local/pgsql/.psql_history
测试一下:
切换到postgres用户(此时是在刚刚使用tar命令解压的目录进行)
#su postgres
$cd
创建数据库
$createdb test
进入数据库test
$psql test
创建test表
test=#create table test(id int);
CREATE TABLE
退出test数据库
test=#\q;
从$回到root权限使用命令:
$su -
出现输入密码选项,输入root密码回车就切换到root
进入到postgres数据库
postgres=#
查看所有的数据库命令(可以列出所有数据库)
postgres=#\l
或者
postgres=# select datname from pg_database;
切换数据库命令
postgres=# \c test
列出当前数据库中所有表的命令:
test=# \dt
结果如下:
List of relationsSchema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
查看postgresql数据库状态
ps -ef | grep postgres
查看自定义用户,第三个号大于500的就是自定义用户
# vim /etc/passwd
查看服务启动状态:(如果有多个postgres说明已经启动)
ps aux | grep postgres