CentOS的源中自带有PostgreSQL,能够经过 yum list | grep postgresql 查看系统自带的版本。php
一、安装 yum 源(地址从 http://yum.postgresql.org/repopackages.php 获取)sql
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
二、安装PostgreSQL数据库
这里最核心的是要安装postgresql96-server和postgresql96-contrib,其中”contrib”包里包含了一些经常使用的组件和方法。centos
yum install postgresql96-server postgresql96-contrib
安装后,可执行文件在 /usr/pgsql-9.6/bin/, 数据和配置文件在 /var/lib/pgsql/9.6/data/bash
三、初始化数据库服务器
/usr/pgsql-9.6/bin/postgresql96-setup initdb
若是指定数据目录执行下列命令:dom
[root@localhost ~]# sudo -i -u postgres /usr/pgsql-9.6/bin/initdb -D /data/pg/pgdata
四、默认状况下PostgreSQL不支持密码登陆,如需支持须要修改配置文件socket
vi /var/lib/pgsql/9.6/data/pg_hba.conf
将未注释行中的ident 替换为 md5ide
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 ident # IPv6 local connections: host all all ::1/128 ident # Allow replication connections from localhost, by a user with the # replication privilege. #local replication postgres peer #host replication postgres 127.0.0.1/32 ident #host replication postgres ::1/128 ident
如需开启远程访问,可编辑/var/lib/pgsql/9.6/data/postgresql.conf 文件。post
将 #listen_addresses = 'localhost' 修改成 listen_addresses='' (此处‘’也能够改成你想开放的服务器IP)
另外对于特定的IP还能够设置开启信任远程链接,修改/var/lib/pgsql/9.6/data/pg_hba.conf,按下面的格式进行添加设置。
# IPv4 local connections: host all all 127.0.0.1/32 trust host all all 8.8.8.8/32(须要链接的服务器IP) trust
修改完配置之后不要忘了重启服务。
五、管理服务
systemctl start postgresql-9.6 #启动服务 systemctl restart postgresql-9.6 #重启服务 systemctl stop postgresql-9.6 #中止服务 systemctl enable postgresql-9.6 #自动启动
六、登陆PostgreSQL
PostgreSQL 安装完成后,会创建一个‘postgres’用户,用于执行PostgreSQL,数据库中也会创建一个’postgres’用户,若是咱们要使用PostgreSQL就必须先登陆此账号。
sudo -i -u postgres
执行后提示符会变为 ‘-bash-4.2$’,再运行
同构执行进入 psql 进入postgresql命令行环境。
[root[@localhost~]# sudo -i -u postgres -bash-4.2$ psql psql (9.6.1) Type "help" for help. postgres=#
接着能够执行 ALTER USER postgres WITH PASSWORD '123456' 来设置postgres用户密码,可经过 \q 退出数据库。
七、打开防火墙
CentOS 防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,只需以服务方式将PostgreSQL服务开放便可。
firewall-cmd --add-service=postgresql --permanent 开放postgresql服务 firewall-cmd --reload 重载防火墙