Ubuntu 上安装开源数据库 PostgreSQL 9.4 和 phpPgAdmin

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. <span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install postgresql postgresql</span><span class="pun">-</span><span class="pln">contrib</span>

如果你需要其它的版本,按照下面那样先添加 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. <span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">sources</span><span class="pun">.</span><span class="kwd">list</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">pgdg</span><span class="pun">.</span><span class="kwd">list</span>

用下面一行添加仓库:

  1. <span class="pln">deb http</span><span class="pun">:</span><span class="com">//apt.postgresql.org/pub/repos/apt/ utopic-pgdg main</span>

注意: 上面的库只能用于 Ubuntu 14.10。还没有升级到 Ubuntu 15.04 和 15.10。

对于 Ubuntu 14.04,添加下面一行:

  1. <span class="pln">deb http</span><span class="pun">:</span><span class="com">//apt.postgresql.org/pub/repos/apt/ trusty-pgdg main</span>

对于 Ubuntu 12.04,添加下面一行:

  1. <span class="pln">deb http</span><span class="pun">:</span><span class="com">//apt.postgresql.org/pub/repos/apt/ precise-pgdg main</span>

导入库签名密钥:

  1. <span class="kwd">wget</span><span class="pun">--</span><span class="pln">quiet </span><span class="pun">-</span><span class="pln">O </span><span class="pun">-</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//www.postgresql.org/media/keys/ACCC4CF8.asc</span>
  2. <span class="kwd">sudo</span><span class="pln"> apt</span><span class="pun">-</span><span class="pln">key add </span><span class="pun">-</span>

更新软件包列表:

  1. <span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> update</span>

然后安装需要的版本。

  1. <span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install postgresql</span><span class="pun">-</span><span class="lit">9.4</span>

 

访问 PostgreSQL 命令窗口

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

  1. <span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u postgres psql postgres</span>

 

示例输出:

  1. <span class="pln">psql </span><span class="pun">(</span><span class="lit">9.4</span><span class="pun">.</span><span class="lit">5</span><span class="pun">)</span>
  2. <span class="typ">Type</span><span class="str">"help"</span><span class="kwd">for</span><span class="pln"> help</span><span class="pun">.</span>
  3. <span class="pln">postgres</span><span class="pun">=#</span>

要退出 postgresql 窗口,在 psql 窗口输入 \q 退出到终端。

设置 “postgres” 用户密码

登录到 postgresql 窗口,

  1. <span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u postgres psql postgres</span>

用下面的命令为用户 postgres 设置密码:

  1. <span class="pln">postgres</span><span class="pun">=#</span><span class="pln"> \password postgres </span>
  2. <span class="typ">Enter</span><span class="kwd">new</span><span class="pln"> password</span><span class="pun">:</span>
  3. <span class="typ">Enter</span><span class="pln"> it again</span><span class="pun">:</span>
  4. <span class="pln">postgres</span><span class="pun">=#</span><span class="pln"> \q</span>

要安装 PostgreSQL Adminpack 扩展,在 postgresql 窗口输入下面的命令:

  1. <span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u postgres psql postgres</span>

  1. <span class="pln">postgres</span><span class="pun">=#</span><span class="pln"> CREATE EXTENSION adminpack</span><span class="pun">;</span>
  2. <span class="pln">CREATE EXTENSION</span>

psql 窗口输入 \q 从 postgresql 窗口退回到终端。

 

创建新用户和数据库

例如,让我们创建一个新的用户,名为 “senthil”,密码是 “ubuntu”,以及名为 “mydb” 的数据库。

  1. <span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u postgres createuser </span><span class="pun">-</span><span class="pln">D </span><span class="pun">-</span><span class="pln">A </span><span class="pun">-</span><span class="pln">P senthil</span>

  1. <span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u postgres createdb </span><span class="pun">-</span><span class="pln">O senthil mydb</span>

 

删除用户和数据库

要删除数据库,首先切换到 postgres 用户:

  1. <span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u postgres psql postgres</span>

输入命令:

  1. <span class="pln">$ drop database </span><span class="pun"><</span><span class="pln">database</span><span class="pun">-</span><span class="pln">name</span><span class="pun">></span>

要删除一个用户,输入下面的命令:

  1. <span class="pln">$ drop user </span><span class="pun"><</span><span class="pln">user</span><span class="pun">-</span><span class="pln">name</span><span class="pun">></span>

 

配置 PostgreSQL-MD5 验证

MD5 验证 要求用户提供一个 MD5 加密的密码用于认证。首先编辑 /etc/postgresql/9.4/main/pg_hba.conf 文件:

  1. <span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postgresql</span><span class="pun">/</span><span class="lit">9.4</span><span class="pun">/</span><span class="pln">main</span><span class="pun">/</span><span class="pln">pg_hba</span><span class="pun">.</span><span class="pln">conf</span>

按照下面所示添加或修改行

  1. <span class="pun">[...]</span>
  2. <span class="com">#</span><span class="pln"> TYPE DATABASE USER ADDRESS METHOD</span>
  3. <span class="com">#</span><span class="str">"local"</span><span class="kwd">is</span><span class="kwd">for</span><span class="typ">Unix</span><span class="pln"> domain socket connections only</span>
  4. <span class="kwd">local</span><span class="pln"> all all md5</span>
  5. <span class="com">#</span><span class="typ">IPv4</span><span class="kwd">local</span><span class="pln"> connections</span><span class="pun">:</span>
  6. <span class="pln">host all all </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">/</span><span class="lit">32</span><span class="pln"> md5</span>
  7. <span class="pln">host all all </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">1.0</span><span class="pun">/</span><span class="lit">24</span><span class="pln"> md5</span>
  8. <span class="com">#</span><span class="typ">IPv6</span><span class="kwd">local</span><span class="pln"> connections</span><span class="pun">:</span>
  9. <span class="pln">host all all </span><span class="pun">::</span><span class="lit">1</span><span class="pun">/</span><span class="lit">128</span><span class="pln"> md5</span>
  10. <span class="pun">[...]</span>

其中, 192.168.1.0/24 是我的本地网络 IP 地址。用你自己的地址替换。

重启 postgresql 服务以使更改生效:

  1. <span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> restart postgresql</span>

或者,

  1. <span class="kwd">sudo</span><span class="pln"> service postgresql restart</span>

 

配置 PostgreSQL TCP/IP 配置

默认情况下,没有启用 TCP/IP 连接,因此其它计算机的用户不能访问 postgresql。为了允许其它计算机的用户访问,编辑文件 /etc/postgresql/9.4/main/postgresql.conf:

  1. <span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postgresql</span><span class="pun">/</span><span class="lit">9.4</span><span class="pun">/</span><span class="pln">main</span><span class="pun">/</span><span class="pln">postgresql</span><span class="pun">.</span><span class="pln">conf</span>

找到下面一行:

  1. <span class="pun">[...]</span>
  2. <span class="com">#</span><span class="pln">listen_addresses </span><span class="pun">=</span><span class="str">'localhost'</span>
  3. <span class="pun">[...]</span>
  4. <span class="com">#</span><span class="pln">port </span><span class="pun">=</span><span class="lit">5432</span>
  5. <span class="pun">[...]</span>

取消该行的注释,然后设置你 postgresql 服务器的 IP 地址,或者设置为 ‘*’ 监听所有用户。你应该谨慎设置所有远程用户都可以访问 PostgreSQL。

  1. <span class="pun">[...]</span>
  2. <span class="pln">listen_addresses </span><span class="pun">=</span><span class="str">'*'</span>
  3. <span class="pun">[...]</span>
  4. <span class="pln">port </span><span class="pun">=</span><span class="lit">5432</span>
  5. <span class="pun">[...]</span>

重启 postgresql 服务保存更改:

  1. <span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> restart postgresql</span>

或者,

  1. <span class="kwd">sudo</span><span class="pln"> service postgresql restart</span>

 

用 phpPgAdmin 管理 PostgreSQL

phpPgAdmin 是基于 web 用 PHP 写的 PostgreSQL 管理工具。

默认仓库中有可用的 phpPgAdmin。用下面的命令安装 phpPgAdmin:

  1. <span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install phppgadmin</span>

默认情况下,你可以在本地系统的 web 浏览器用 http://localhost/phppgadmin 访问 phppgadmin。

要访问远程系统,在 Ubuntu 15.10 上做如下操作:

编辑文件 /etc/apache2/conf-available/phppgadmin.conf,

  1. <span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">-</span><span class="pln">available</span><span class="pun">/</span><span class="pln">phppgadmin</span><span class="pun">.</span><span class="pln">conf</span>

找到 Require local 的一行在这行前面添加 # 注释掉它。

  1. <span class="com">#</span><span class="typ">Require</span><span class="kwd">local</span>

添加下面的一行:

  1. <span class="pln">allow </span><span class="kwd">from</span><span class="pln"> all</span>

保存并退出文件。

然后重启 apache 服务。

  1. <span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> restart apache2</span>

对于 Ubuntu 14.10 及之前版本:

编辑 /etc/apache2/conf.d/phppgadmin:

  1. <span class="kwd">sudo</span><span class="kwd">nano</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">phppgadmin</span>

注释掉下面一行:

  1. <span class="pun">[...]</span>
  2. <span class="com">#</span><span class="pln">allow </span><span class="kwd">from</span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">/</span><span class="lit">255.0</span><span class="pun">.</span><span class="lit">0.0</span><span class="pun">::</span><span class="lit">1</span><span class="pun">/</span><span class="lit">128</span>

取消下面一行的注释使所有系统都可以访问 phppgadmin。

  1. <span class="pln">allow </span><span class="kwd">from</span><span class="pln"> all</span>

编辑 /etc/apache2/apache2.conf:

  1. <span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">.</span><span class="pln">conf</span>

添加下面一行:

  1. <span class="typ">Include</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">conf</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">phppgadmin</span>

然后重启 apache 服务。

  1. <span class="kwd">sudo</span><span class="pln"> service apache2 restart</span>

 

配置 phpPgAdmin

编辑文件 /etc/phppgadmin/config.inc.php, 做以下更改。下面大部分选项都带有解释。认真阅读以便了解为什么要更改这些值。

  1. <span class="kwd">sudo</span><span class="kwd">nano</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">phppgadmin</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">inc</span><span class="pun">.</span><span class="pln">php</span>

找到下面一行:

  1. <span class="pln">$conf</span><span class="pun">[</span><span class="str">'servers'</span><span class="pun">][</span><span class="lit">0</span><span class="pun">][</span><span class="str">'host'</span><span class="pun">]</span><span class="pun">=</span><span class="str">''</span><span class="pun">;</span>

按照下面这样更改:

  1. <span class="pln">$conf</span><span class="pun">[</span><span class="str">'servers'</span><span class="pun">][</span><span class="lit">0</span><span class="pun">][</span><span class="str">'host'</span><span class="pun">]</span><span class="pun">=</span><span class="str">'localhost'</span><span class="pun">;</span>

找到这一行:

  1. <span class="pln">$conf</span><span class="pun">[</span><span class="str">'extra_login_security'</span><span class="pun">]</span><span class="pun">=</span><span class="kwd">true</span><span class="pun">;</span>

更改值为 false

  1. <span class="pln">$conf</span><span class="pun">[</span><span class="str">'extra_login_security'</span><span class="pun">]</span><span class="pun">=</span><span class="kwd">false</span><span class="pun">;</span>

找到这一行:

  1. <span class="pln">$conf</span><span class="pun">[</span><span class="str">'owned_only'</span><span class="pun">]</span><span class="pun">=</span><span class="kwd">false</span><span class="pun">;</span>

更改值为 true

  1. <span class="pln">$conf</span><span class="pun">[</span><span class="str">'owned_only'</span><span class="pun">]</span><span class="pun">=</span><span class="kwd">true</span><span class="pun">;</span>

保存并关闭文件。重启 postgresql 服务和 Apache 服务。

  1. <span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> restart postgresql</span>
  2. <span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> restart apache2</span>

或者,

  1. <span class="kwd">sudo</span><span class="pln"> service postgresql restart</span>
  2. <span class="kwd">sudo</span><span class="pln"> service apache2 restart</span>

现在打开你的浏览器并导航到 http://ip-address/phppgadmin。你会看到以下截图。

Ubuntu 上安装开源数据库 PostgreSQL 9.4 和 phpPgAdmin

phpPgAdmin

用你之前创建的用户登录。我之前已经创建了一个名为 “senthil” 的用户,密码是 “ubuntu”,因此我以 “senthil” 用户登录。

Ubuntu 上安装开源数据库 PostgreSQL 9.4 和 phpPgAdmin

phpPgAdmin

然后你就可以访问 phppgadmin 面板了。

Ubuntu 上安装开源数据库 PostgreSQL 9.4 和 phpPgAdmin

phpPgAdmin

用 postgres 用户登录:

Ubuntu 上安装开源数据库 PostgreSQL 9.4 和 phpPgAdmin

phpPgAdmin

就是这样。现在你可以用 phppgadmin 可视化创建、删除或者更改数据库了。

加油!

------------------------------------华丽丽的分割线------------------------------------

------------------------------------华丽丽的分割线------------------------------------

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

via: http://www.unixmen.com/install-postgresql-9-4-and-phppgadmin-on-ubuntu-15-10/

作者:SK 译者:ictlyh 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐