Welcome 微信登录

首页 / 数据库 / MySQL / Ubuntu 上安装开源数据库 PostgreSQL 9.4 和 phpPgAdmin

简介

PostgreSQL 是一款强大的,开源的,对象关系型数据库系统。它支持所有的主流操作系统,包括 Linux、Unix(AIX、BSD、HP-UX,SGI IRIX、Mac OS、Solaris、Tru64) 以及 Windows 操作系统。下面是 Ubuntu 发起者 Mark Shuttleworth 对 PostgreSQL 的一段评价。
PostgreSQL 是一款极赞的数据库系统。刚开始我们在 Launchpad 上使用它的时候,并不确定它能否胜任工作。但我是错了。它很强壮、快速,在各个方面都很专业。— Mark Shuttleworth.
在这篇简短的指南中,让我们来看看如何在 Ubuntu 15.10 服务器中安装 PostgreSQL 9.4。 

安装 PostgreSQL

默认仓库中就有可用的 PostgreSQL。在终端中输入下面的命令安装它。
  1. sudoapt-get install postgresql postgresql-contrib
如果你需要其它的版本,按照下面那样先添加 PostgreSQL 仓库然后再安装。PostgreSQL apt 仓库 支持 amd64 和 i386 架构的 Ubuntu 长期支持版(10.04、12.04 和 14.04),以及非长期支持版(14.04)。对于其它非长期支持版,该软件包虽然没有完全支持,但使用和 LTS 版本近似的也能正常工作。 

Ubuntu 14.10 系统:

新建文件/etc/apt/sources.list.d/pgdg.list
  1. sudovi/etc/apt/sources.list.d/pgdg.list
用下面一行添加仓库:
  1. deb http://apt.postgresql.org/pub/repos/apt/ utopic-pgdg main
注意: 上面的库只能用于 Ubuntu 14.10。还没有升级到 Ubuntu 15.04 和 15.10。对于 Ubuntu 14.04,添加下面一行:
  1. deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
对于 Ubuntu 12.04,添加下面一行:
  1. deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main
导入库签名密钥:
  1. wget--quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc
  2. sudo apt-key add -
更新软件包列表:
  1. sudoapt-get update
然后安装需要的版本。
  1. sudoapt-get install postgresql-9.4
 

访问 PostgreSQL 命令窗口

默认的数据库名称和数据库用户名称都是 “postgres”。切换到 postgres 用户进行 postgresql 相关的操作:
  1. sudo-u postgres psql postgres
 

示例输出:

  1. psql (9.4.5)
  2. Type"help"for help.
  3. postgres=#
要退出 postgresql 窗口,在 psql 窗口输入 q 退出到终端。

设置 “postgres” 用户密码

登录到 postgresql 窗口,
  1. sudo-u postgres psql postgres
用下面的命令为用户 postgres 设置密码:
  1. postgres=# password postgres
  2. Enternew password:
  3. Enter it again:
  4. postgres=# q
要安装 PostgreSQL Adminpack 扩展,在 postgresql 窗口输入下面的命令:
  1. sudo-u postgres psql postgres

  1. postgres=# CREATE EXTENSION adminpack;
  2. CREATE EXTENSION
psql 窗口输入 q 从 postgresql 窗口退回到终端。 

创建新用户和数据库

例如,让我们创建一个新的用户,名为 “senthil”,密码是 “ubuntu”,以及名为 “mydb” 的数据库。
  1. sudo-u postgres createuser -D -A -P senthil

  1. sudo-u postgres createdb -O senthil mydb
 

删除用户和数据库

要删除数据库,首先切换到 postgres 用户:
  1. sudo-u postgres psql postgres
输入命令:
  1. $ drop database <database-name>
要删除一个用户,输入下面的命令:
  1. $ drop user <user-name>
 

配置 PostgreSQL-MD5 验证

MD5 验证 要求用户提供一个 MD5 加密的密码用于认证。首先编辑 /etc/postgresql/9.4/main/pg_hba.conf 文件:
  1. sudovi/etc/postgresql/9.4/main/pg_hba.conf
按照下面所示添加或修改行
  1. [...]
  2. # TYPE DATABASE USER ADDRESS METHOD
  3. #"local"isforUnix domain socket connections only
  4. local all all md5
  5. #IPv4local connections:
  6. host all all 127.0.0.1/32 md5
  7. host all all 192.168.1.0/24 md5
  8. #IPv6local connections:
  9. host all all ::1/128 md5
  10. [...]
其中, 192.168.1.0/24 是我的本地网络 IP 地址。用你自己的地址替换。重启 postgresql 服务以使更改生效:
  1. sudosystemctl restart postgresql
或者,
  1. sudo service postgresql restart
 

配置 PostgreSQL TCP/IP 配置

默认情况下,没有启用 TCP/IP 连接,因此其它计算机的用户不能访问 postgresql。为了允许其它计算机的用户访问,编辑文件 /etc/postgresql/9.4/main/postgresql.conf:
  1. sudovi/etc/postgresql/9.4/main/postgresql.conf
找到下面一行:
  1. [...]
  2. #listen_addresses ="localhost"
  3. [...]
  4. #port =5432
  5. [...]
取消该行的注释,然后设置你 postgresql 服务器的 IP 地址,或者设置为 ‘*’ 监听所有用户。你应该谨慎设置所有远程用户都可以访问 PostgreSQL。
  1. [...]
  2. listen_addresses ="*"
  3. [...]
  4. port =5432
  5. [...]
重启 postgresql 服务保存更改:
  1. sudosystemctl restart postgresql
或者,
  1. sudo service postgresql restart
 

用 phpPgAdmin 管理 PostgreSQL

phpPgAdmin 是基于 web 用 PHP 写的 PostgreSQL 管理工具。默认仓库中有可用的 phpPgAdmin。用下面的命令安装 phpPgAdmin:
  1. sudoapt-get install phppgadmin
默认情况下,你可以在本地系统的 web 浏览器用 http://localhost/phppgadmin 访问 phppgadmin。要访问远程系统,在 Ubuntu 15.10 上做如下操作:编辑文件 /etc/apache2/conf-available/phppgadmin.conf,
  1. sudovi/etc/apache2/conf-available/phppgadmin.conf
找到 Require local 的一行在这行前面添加 # 注释掉它。
  1. #Requirelocal
添加下面的一行:
  1. allow from all
保存并退出文件。然后重启 apache 服务。
  1. sudosystemctl restart apache2
对于 Ubuntu 14.10 及之前版本:编辑 /etc/apache2/conf.d/phppgadmin:
  1. sudonano/etc/apache2/conf.d/phppgadmin
注释掉下面一行:
  1. [...]
  2. #allow from127.0.0.0/255.0.0.0::1/128
取消下面一行的注释使所有系统都可以访问 phppgadmin。
  1. allow from all
编辑 /etc/apache2/apache2.conf:
  1. sudovi/etc/apache2/apache2.conf
添加下面一行:
  1. Include/etc/apache2/conf.d/phppgadmin
然后重启 apache 服务。
  1. sudo service apache2 restart
 

配置 phpPgAdmin

编辑文件 /etc/phppgadmin/config.inc.php, 做以下更改。下面大部分选项都带有解释。认真阅读以便了解为什么要更改这些值。
  1. sudonano/etc/phppgadmin/config.inc.php
找到下面一行:
  1. $conf["servers"][0]["host"]="";
按照下面这样更改:
  1. $conf["servers"][0]["host"]="localhost";
找到这一行:
  1. $conf["extra_login_security"]=true;
更改值为 false
  1. $conf["extra_login_security"]=false;
找到这一行:
  1. $conf["owned_only"]=false;
更改值为 true
  1. $conf["owned_only"]=true;
保存并关闭文件。重启 postgresql 服务和 Apache 服务。
  1. sudosystemctl restart postgresql
  2. sudosystemctl restart apache2
或者,
  1. sudo service postgresql restart
  2. sudo service apache2 restart
现在打开你的浏览器并导航到 http://ip-address/phppgadmin。你会看到以下截图。phpPgAdmin 用你之前创建的用户登录。我之前已经创建了一个名为 “senthil” 的用户,密码是 “ubuntu”,因此我以 “senthil” 用户登录。phpPgAdmin 然后你就可以访问 phppgadmin 面板了。phpPgAdmin 用 postgres 用户登录:phpPgAdmin 就是这样。现在你可以用 phppgadmin 可视化创建、删除或者更改数据库了。加油!------------------------------------华丽丽的分割线------------------------------------Ubuntu Server 14.04 下安装 PostgreSQL 9.3.5 数据库  http://www.linuxidc.com/Linux/2015-12/126175.htmCentOS 6.3环境下yum安装PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htmPostgreSQL缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htmWindows平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htmUbuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htmUbuntu上的phppgAdmin安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htmCentOS平台下安装PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htmPostgreSQL配置Streaming Replication集群 http://www.linuxidc.com/Linux/2014-05/101724.htm如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin  http://www.linuxidc.com/Linux/2014-12/110108.htm------------------------------------华丽丽的分割线------------------------------------PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里via: http://www.unixmen.com/install-postgresql-9-4-and-phppgadmin-on-ubuntu-15-10/作者:SK 译者:ictlyh 校对:wxy本文由 LCTT 原创编译,Linux中国 荣誉推出本文永久更新链接地址