centos6.4 安装postgresql9.2.4教程
1、登陆Centos使用root用户登陆
首先卸载旧版的postgresql:
yum remove postgresql*
下载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(#userdel 用户名)用户组添加和删除(#groupadd 用户组名,#groupdel 用户组名)
修改用户密码(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用户:
$su root
复制安装目录下的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
可以使用pg_ctl start(pg_ctl stop)命令启动(停止)postgres服务命令如下:
启动
$pg_ctl start
停止
$pg_ctl stop
创建数据库
$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 relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
查看postgresql数据库状态
ps -ef | grep postgres
查看自定义用户,第三个号大于500的就是自定义用户
# vim /etc/passwd
查看服务启动状态:(如果有多个postgres说明已经启动)
ps aux | grep postgres
至此,postgresql数据库完全搭建完成
linux查看连接用户
#w
开启postgresql允许远程连接配置:
安 装PostgreSQL数据库之后,默认是只接受本地访问连接。如果想在其他主机上访问PostgreSQL数据库服务器,就需要进行相应的配置。
配置远 程连接PostgreSQL数据库的步骤很简单,只需要修改data目录下的pg_hba.conf(配置数据库的访问权限)和postgresql.conf(配置PostgreSQL数据库服务器的相应的参数)。
1.修改pg_hba.conf文件,配置用户的访问权限(#开头的行是注释内容):
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 10.118.1.0/24 md5
# IPv6 local connections:
host all all ::1/128 trust
其中,# IPv4 local connections下面添加内容,表示允许网段10.118.1.0上的所有主机使用所有合法的数据库用户名访问数据库,并提供加密的密码验证。
其中,数字24是子网掩码,表示允许10.118.1.0--10.118.1.255的计算机访问!
不过一般的更改,不需要特别严格的权限控制时,IPv4中换成
host all all 0.0.0.0/0 trust 即可。
2.修改postgresql.conf文件,将数据库服务器的监听模式修改为监听所有主机发出的连接请求。
定位到#listen_addresses=’localhost’。PostgreSQL安装完成后,默认是只接受来在本机localhost的连接请 求。
将行开头都#去掉,将行内容修改为listen_addresses=’*'来允许数据库服务器监听来自任何主机的连接请求