Freud's Blog

Stay hungry, stay foolish. 少年辛苦终身事,莫向光阴惰寸功。

Postgresql在Linux环境下yum方式安装和配置

Posted on By Freud Kang

Yum安装

  • 安装Yum源
[root@localhost ~]# yum install https://download.postgresql.org/pub/repos/yum/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-3.noarch.rpm
  • Yum查询下是否有可用的安装包
[root@localhost ~]# yum search postgresql93
  • Yum安装
[root@localhost ~]# yum install postgresql93-server postgresql93-contrib

初始化数据库,并启动数据库

[root@localhost ~]# service postgresql-9.3 initdb
[root@localhost ~]# service postgresql-9.3 start

把PostgreSQL 服务加入到启动列表

[root@localhost ~]# chkconfig postgresql-9.3 on 
[root@localhost ~]# chkconfig --list|grep postgres

修改PostgreSQL 数据库用户postgres的密码, 并创建数据库和测试数据

[root@localhost ~]# su postgres 
[root@localhost ~]# psql 
# ALTER USER postgres WITH PASSWORD 'postgres';
# select * from pg_shadow ;	
# create database david;
# \c david
# create table test (id integer, name text);
# insert into test values (1,'david');
# select * from test ; 

修改linux 系统用户postgres 的密码

[root@localhost ~]# passwd postgres

修改PostgresSQL 数据库配置实现远程访问

  • 修改postgresql.conf 文件,如果想让PostgreSQL 监听整个网络的话,将listen_addresses 前的#去掉,并将 listen_addresses = ‘localhost’ 改成 listen_addresses = ‘*’
[root@localhost ~]# vi /var/lib/pgsql/9.3/data/postgresql.conf
  • 修改客户端认证配置文件pg_hba.conf,在IPV4下面添加一条记录”host all all 10.1.5.0/24 md5”,其中10.1.5.0代表网段’10.1.5.*’, 24是子网掩码,代表10.1.5.0-10.1.5.255, md5代表使用md5加密
[root@localhost ~]# vi /var/lib/pgsql/9.3/data/pg_hba.conf
#信任10.1.5.*网段所有IP
host    all             all             10.1.5.0/24         md5
#信任所有IP
host    all             all             0.0.0.0/0           trust 
  • 重启postgresql
[root@localhost ~]# service postgresql-9.3 restart
  • 远程连接测试,默认端口是5432 - 成功



参考资料:

CentOS 6.3下PostgreSQL 的安装与配置:http://www.cnblogs.com/mchina/archive/2012/06/06/2539003.html

PostgreSQL 9.4.5 Documentation : http://www.postgresql.org/docs/9.4/interactive/index.html

Linux downloads (Red Hat family) : http://www.postgresql.org/download/linux/redhat/