CentOS 7下安装PostgreSQL 9.6的教程分享
前言
PostgreSQL是一个强大开源的对象关系类型数据库系统,它能运行于几乎所有主要的操作系统,包括Linux、Unix(AIX、BSD、HP-UX、SGIIRIX、MacOS、Solaris、Tru64)、WindowsOS。对于新的项目我是非常建议使用PostgreSQL。这里就简单的整理下CentOS下的安装过程。
CentOS的源中自带有PostgreSQL,可以通过yumlist|greppostgresql查看系统自带的版本,我这边看到的是9.2版本,个人还是比较建议安装更新的版本。
安装
1、安装yum源(地址从http://yum.postgresql.org/repopackages.php获取)
yuminstallhttps://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
这里选择的是最新的9.6版本,安装完毕后使用yumupdate更新源信息,然后再使用yumlist|greppostgresql,是否已经有最新版本的postgresql。
[root@localhost~]#yumlist|greppostgresql calligra-kexi-driver-postgresql.x86_642.9.10-1.el7epel collectd-postgresql.x86_645.6.0-1.el7epel freeradius-postgresql.x86_643.0.4-6.el7base libreoffice-postgresql.x86_641:4.3.7.2-5.el7_2.1updates mingw32-postgresql.noarch9.3.4-2.el7epel mingw64-postgresql.noarch9.3.4-2.el7epel opendbx-postgresql.x86_641.4.6-6.el7epel opensips-postgresql.x86_641.10.5-3.el7epel owncloud-postgresql.noarch9.0.4-1.el7epel pcp-pmda-postgresql.x86_643.10.6-2.el7base pdns-backend-postgresql.x86_643.4.10-1.el7epel perdition-postgresql.x86_642.1-5.el7epel postgresql.i6869.2.15-1.el7_2updates postgresql.x86_649.2.15-1.el7_2updates postgresql-contrib.x86_649.2.15-1.el7_2updates postgresql-devel.i6869.2.15-1.el7_2updates postgresql-devel.x86_649.2.15-1.el7_2updates postgresql-docs.x86_649.2.15-1.el7_2updates postgresql-jdbc.noarch9.4.1212-1.rhel7pgdg96 postgresql-jdbc-javadoc.noarch9.4.1212-1.rhel7pgdg96 postgresql-libs.i6869.2.15-1.el7_2updates postgresql-libs.x86_649.2.15-1.el7_2updates postgresql-odbc.x86_6409.03.0100-2.el7base postgresql-pgpool-II.x86_643.4.6-1.el7epel postgresql-pgpool-II-devel.x86_643.4.6-1.el7epel postgresql-pgpool-II-extensions.x86_643.4.6-1.el7epel postgresql-plperl.x86_649.2.15-1.el7_2updates postgresql-plpython.x86_649.2.15-1.el7_2updates postgresql-plruby.x86_640.5.3-13.el7epel postgresql-plruby-doc.x86_640.5.3-13.el7epel postgresql-pltcl.x86_649.2.15-1.el7_2updates postgresql-server.x86_649.2.15-1.el7_2updates postgresql-test.x86_649.2.15-1.el7_2updates postgresql-unit96.x86_641.0-1.rhel7pgdg96 postgresql-unit96-debuginfo.x86_641.0-1.rhel7pgdg96 postgresql-upgrade.x86_649.2.15-1.el7_2updates postgresql96.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-contrib.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-debuginfo.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-devel.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-docs.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-libs.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-odbc.x86_6409.05.0400-1PGDG.rhel7pgdg96 postgresql96-plperl.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-plpython.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-pltcl.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-python.x86_644.2-1PGDG.rhel7pgdg96 postgresql96-python-debuginfo.x86_644.2-1PGDG.rhel7pgdg96 postgresql96-server.x86_649.6.1-1PGDG.rhel7pgdg96 postgresql96-tcl.x86_642.1.1-1.rhel7pgdg96 postgresql96-tcl-debuginfo.x86_642.1.1-1.rhel7pgdg96 postgresql96-test.x86_649.6.1-1PGDG.rhel7pgdg96 proftpd-postgresql.x86_641.3.5b-2.el7epel python-testing.postgresql.noarch1.1.0-2.el7epel qt-postgresql.i6861:4.8.5-12.el7_2updates qt-postgresql.x86_641:4.8.5-12.el7_2updates qt5-qtbase-postgresql.x86_645.6.1-3.el7epel soci-postgresql.x86_643.2.3-1.el7epel soci-postgresql-devel.x86_643.2.3-1.el7epel
2、安装PostgreSQL
这里最核心的是要安装postgresql96-server和postgresql96-contrib,其中”contrib”包里包含了一些常用的组件和方法。
yuminstallpostgresql96-serverpostgresql96-contrib
安装后,可执行文件在/usr/pgsql-9.5/bin/,数据和配置文件在/var/lib/pgsql/9.6/data/
3、初始化数据库
/usr/pgsql-9.6/bin/postgresql96-setupinitdb
4、默认情况下PostgreSQL不支持密码登录,如需支持需要修改配置文件
vi/var/lib/pgsql/9.6/data/pg_hba.conf
将未注释行中的ident替换为md5
#TYPEDATABASEUSERADDRESSMETHOD #"local"isforUnixdomainsocketconnectionsonly localallallpeer #IPv4localconnections: hostallall127.0.0.1/32ident #IPv6localconnections: hostallall::1/128ident #Allowreplicationconnectionsfromlocalhost,byauserwiththe #replicationprivilege. #localreplicationpostgrespeer #hostreplicationpostgres127.0.0.1/32ident #hostreplicationpostgres::1/128ident
如需开启远程访问,可编辑/var/lib/pgsql/9.6/data/postgresql.conf文件。
将#listen_addresses='localhost' 修改为listen_addresses='*'(当然,此处‘*'也可以改为任何你想开放的服务器IP)
另外对于特定的IP还可以设置开启信任远程连接,修改/var/lib/pgsql/9.6/data/pg_hba.conf,按下面的格式进行添加设置。
#IPv4localconnections: hostallall127.0.0.1/32trust hostallall8.8.8.8/32(需要连接的服务器IP)trust
修改完配置以后不要忘了重启服务。
5、管理服务,和其他服务一样还是用systemd进行管理
systemctlstartpostgresql-9.6#启动服务 systemctlrestartpostgresql-9.6#重启服务 systemctlstoppostgresql-9.6#停止服务 systemctlenablepostgresql-9.6#自动启动
6、登录PostgreSQL
PostgreSQL安装完成后,会建立一下‘postgres'用户,用于执行PostgreSQL,数据库中也会建立一个'postgres'用户,如果我们要使用PostgreSQL就必须先登录此帐号。
sudo-i-upostgres
执行后提示符会变为‘-bash-4.2$',再运行
同构执行进入psql进入postgresql命令行环境。
[root@localhost~]#sudo-i-upostgres -bash-4.2$psql psql(9.6.1) Type"help"forhelp. postgres=#
接着可以执行ALTERUSERpostgresWITHPASSWORD'123456'来设置postgres用户密码,可通过\q 退出数据库。
7、打开防火墙
CentOS防火墙中内置了PostgreSQL服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,只需以服务方式将PostgreSQL服务开放即可。
firewall-cmd--add-service=postgresql--permanent开放postgresql服务 firewall-cmd--reload重载防火墙
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。