CentOS 7 安装 PostgreSQL 教程

前端之家收集整理的这篇文章主要介绍了CentOS 7 安装 PostgreSQL 教程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

CentOS的源中自带有Postgresql,可以通过 yum list | grep postgresql 查看系统自带的版本。

1、安装 yum 源(地址从 http://yum.postgresql.org/repopackages.php 获取

  1. yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

2、安装Postgresql

这里最核心的是要安装postgresql96-server和postgresql96-contrib,其中”contrib”包里包含了一些常用的组件和方法

  1. yum install postgresql96-server postgresql96-contrib

安装后,可执行文件在 /usr/pgsql-9.6/bin/, 数据和配置文件在 /var/lib/pgsql/9.6/data/

3、初始化数据库

  1. /usr/pgsql-9.6/bin/postgresql96-setup initdb

如果指定数据目录执行下列命令:

  1. [root@localhost ~]# sudo -i -u postgres /usr/pgsql-9.6/bin/initdb -D /data/pg/pgdata

4、默认情况下Postgresql不支持密码登录,如需支持需要修改配置文件

  1. vi /var/lib/pgsql/9.6/data/pg_hba.conf

将未注释行中的ident 替换为 md5

  1. # TYPE DATABASE USER ADDRESS METHOD
  2. # "local" is for Unix domain socket connections only
  3. local all all peer
  4. # IPv4 local connections:
  5. host all all 127.0.0.1/32 ident
  6. # IPv6 local connections:
  7. host all all ::1/128 ident
  8. # Allow replication connections from localhost,by a user with the
  9. # replication privilege.
  10. #local replication postgres peer
  11. #host replication postgres 127.0.0.1/32 ident
  12. #host replication postgres ::1/128 ident

如需开启远程访问,可编辑/var/lib/pgsql/9.6/data/postgresql.conf 文件

将 #listen_addresses = 'localhost' 修改为 listen_addresses='' (此处‘’也可以改为你想开放的服务器IP)

另外对于特定的IP还可以设置开启信任远程连接,修改/var/lib/pgsql/9.6/data/pg_hba.conf,按下面的格式进行添加设置。

  1. # IPv4 local connections:
  2. host all all 127.0.0.1/32 trust
  3. host all all 8.8.8.8/32(需要连接的服务器IP trust

修改完配置以后不要忘了重启服务。

5、管理服务

  1. systemctl start postgresql-9.6 #启动服务
  2. systemctl restart postgresql-9.6 #重启服务
  3. systemctl stop postgresql-9.6 #停止服务
  4. systemctl enable postgresql-9.6 #自动启动

6、登录Postgresql

Postgresql 安装完成后,会建立一个‘postgres’用户,用于执行Postgresql数据库中也会建立一个’postgres’用户,如果我们要使用Postgresql就必须先登录此帐号。

sudo -i -u postgres

执行后提示符会变为 ‘-bash-4.2$’,再运行

同构执行进入 psql 进入postgresql命令行环境。

  1. [root[@localhost~]# sudo -i -u postgres
  2. -bash-4.2$ psql
  3. psql (9.6.1)
  4. Type "help" for help.
  5. postgres=#

接着可以执行 ALTER USER postgres WITH PASSWORD '123456' 来设置postgres用户密码,可通过 \q 退出数据库

7、打开防火墙

CentOS 防火墙中内置了Postgresql服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,只需以服务方式将Postgresql服务开放即可。

  1. firewall-cmd --add-service=postgresql --permanent 开放postgresql服务
  2. firewall-cmd --reload 重载防火墙

猜你在找的CentOS相关文章