ITPub博客

首页 > 数据库 > 数据库开发技术 > CentOS7下安装PostgreSQL 9.4 与 phpPgAdmin

CentOS7下安装PostgreSQL 9.4 与 phpPgAdmin

数据库开发技术 作者:liuliangbin 时间:2016-04-27 14:19:05 0 删除 编辑

CentOS7下安装PostgreSQL 9.4 与 phpPgAdmin

215人阅读 评论(0) 收藏 举报
分类:
centos(11)

1.安装PostgreSQL

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. rpm -Uvh http://yum.postgresql.org/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-1.noarch.rpm  
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. yum install postgresql94-server postgresql94-contrib  
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. /usr/pgsql-9.4/bin/postgresql94-setup initdb  
启动pstgresql
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. systemctl enable postgresql-9.4.service  
  2. systemctl start postgresql-9.4.service  
  3. systemctl status postgresql-9.4.service  
2. 开放端口
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. firewall-cmd --permanent --add-port=5432/tcp  
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. firewall-cmd --permanent --add-port=80/tcp  
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. firewall-cmd --reload  
3.访问PostgreSQL常用的命令提示符

默认情况下数据库名和用户名都是postgres。切换至用户以执行相关操作:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. su postgres  
输入以下命令登陆:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. psql  
会有以下样例输出:

    psql (9.3.5)

    Type "help" for help.

    Postgres=# 

可通过输入\q退出postgresql返回命令终端
4.设置用户密码

登陆至postgres命令提示符界面

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. su – postgres   
  2. psql   
然后使用命令设置密码
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. postgres=# \password postgres  
  2.   
  3. Enter new password:  
  4.   
  5. Enter it again:  
  6.   
  7. postgres=# \q  

输入命令以建立PostgreSQL系统管理工具

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. postgres=# CREATE EXTENSION adminpack;  
  2. CREATE EXTENSION   

5.创建用户和数据库

例如:用户名:senthil    密码:centos  数据库名:mydb

转到postgres用户

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. su postgres  

创建用户senthil
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ createuser senthil  

创建数据库
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ createdb mydb   

现在登陆至psql提示符界面,为用户senthil设置密码及授权对数据库mydb的访问:
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ psql   
  2.   
  3. psql (9.3.5)  
  4.   
  5. Type "help" for help.   
  6.   
  7. postgres=# alter user senthil with encrypted password 'centos';  
  8.   
  9. ALTER ROLE   
  10.   
  11. postgres=# grant all privileges on database mydb to senthil;  
  12.   
  13. GRANT  
  14.   
  15. postgres=#   

6.删除用户和数据库

首先转到postgres界面

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. su postgres  
输入命令
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ dropdb <database-name>  

删除用户名可输入

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ dropuser <user-name>   

7.配置PostgreSQL-MD5认证

MD5认证需要客户端提供一个MD5-encrypted 密码以便身份验证。你需要编辑 /var/lib/pgsql/9.3/data/pg_hba.conf文件:

vi /var/lib/pgsql/9.3/data/pg_hba.conf

添加或修改的行如下:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. [...]  
  2.   
  3. # TYPE  DATABASE          USER    ADDRESS              METHOD  
  4.   
  5.    
  6.   
  7. # "local" is for Unix domain socket connections only  
  8.   
  9. local        all      all                     md5  
  10.   
  11. # IPv4 local connections:  
  12.   
  13. host        all      all    127.0.0.1/32      md5  
  14.   
  15. host        all      all    192.168.1.0/24    md5  
  16.   
  17. # IPv6 local connections:  
  18.   
  19. host        all      all    ::1/128          md5  
  20.   
  21. [...]  

重启postgresql服务以应用更改

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. systemctl restart postgresql-9.3  

8.配置PostgreSQL-Configure TCP/IP

默认情况下,TCP/IP连接是不可行的,所以其他计算机用户不能连接到postgresql。编辑文件 /var/lib/pgsql/9.3/data/postgresql.conf可以允许连接:

vi /var/lib/pgsql/9.3/data/postgresql.conf 

找到下面的行:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. [...]  
  2.   
  3. #listen_addresses = 'localhost’  
  4.   
  5. [...]  
  6.   
  7. #port = 5432  
  8.   
  9. [...]  

把两行都取消并设置为你的postgresql服务器IP地址或设置为“*”监听所有客户端。如下所示:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. listen_addresses = '*'  
  2.   
  3. port = 5432  
重启以应用更改

在CentOS7系统中:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. systemctl restart postgresql-9.3   

9.使用phpPgAdmin管理PostgreSQL

phpPgAdmin是使用PHP编写的基于web的管理工具,用于管理PostgreSQL。它只适用与PostgreSQL RPM库。

如果你没有添加PostgreSQL库,你可以添加EPEL库。

CentOS 7的话参考下面的链接

http://www.unixmen.com/install-epel-repository-centos-rhel-7/

使用命令更新库

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. yum update  

现在输入命令安装phpPgAdmin:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. yum install phpPgAdmin httpd  

注意phpPgAdmin区分大小写,要准确使用上面所示的大小写!

默认你可以使用http://localhost/phpPgAdmin访问phpPgAdmin。若要远程访问需要继续:

编辑文件/etc/httpd/conf.d/phpPgAdmin.conf

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. vi /etc/httpd/conf.d/phpPgAdmin.conf  

修改如下加粗的部分:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. [...]  
  2.   
  3. Alias /phpPgAdmin /usr/share/phpPgAdmin  
  4.   
  5.    
  6.   
  7. <Location /phpPgAdmin>  
  8.   
  9.   <IfModule mod_authz_core.c>  
  10.   
  11.         # Apache 2.4  
  12.   
  13.         Require all granted  
  14.   
  15.         #Require host example.com  
  16.   
  17.   </IfModule>  
  18.   
  19.   <IfModule !mod_authz_core.c>  
  20.   
  21.         # Apache 2.2  
  22.   
  23.         Order deny,allow  
  24.   
  25.         Allow from all  
  26.   
  27.         # Allow from .example.com  
  28.   
  29.     </IfModule>  
  30.   
  31. </Location>  

启动或重启Apache服务

在CentOS 7系统中

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. systemctl enable httpd  
  2.   
  3. systemctl start httpd   

现在打开浏览器并转到http://ip-address/phpPgAdmin。终于看到下面的界面了!

使用你之前创建的用户登录,我的是用户senthil密码CentOS。

你可能会遇到:Login failed。

这是因为SELLinux可能限制用户连接到PostgreSQL,只需输入以下命令更改即可:

setsebool -P httpd_can_network_connect_db 1

现在你应该能正常登录了。

我的phpPgAdimn如下:

OK!现在你就可以使用图形化界面的phpPgAdmin创建、删除和管理数据库了。


原文出处:http://www.linuxidc.com/Linux/2014-12/110108.htm

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/78156/viewspace-2089311/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    12
  • 访问量
    25318