当前位置: 首页 > 科技观察

在Ubuntu上安装开源数据库PostgreSQL9.6

时间:2023-03-15 09:07:26 科技观察

简介  PostgreSQL是一个功能强大的开源对象关系数据库系统。它支持所有主要操作系统,包括Linux、Unix(AIX、BSD、HP-UX、SGIIRIX、MacOS、Solaris、Tru64)和Windows操作系统。  以下是Ubuntu创建者MarkShuttleworth对PostgreSQL的评价。  PostgreSQL是一个很棒的数据库系统。当我们第一次开始在Launchpad上使用它时,我们不确定它是否能胜任这项工作。但是我错了。它在各个方面都强大、快速且专业。  —MarkShuttleworth。  在这篇简短的指南中,让我们来看看如何在Ubuntu15.10服务器中安装PostgreSQL9.4。  安装PostgreSQL  PostgreSQL在默认仓库中可用。在终端中输入以下命令进行安装。sudoapt-getinstallpostgresqlpostgresql-contrib  如果需要其他版本,请先添加PostgreSQL仓库,然后按如下方式安装。  PostgreSQLapt存储库支持UbuntuLTS版本(10.04、12.04和14.04)和适用于amd64和i386架构的非LTS版本(14.10)。对于其他非长期支持的版本,该包并不完全支持,但可以正常使用一个近似的LTS版本。  Ubuntu14.10系统:  新建文件/etc/apt/sources.list.d/pgdg.list;sudovi/etc/apt/sources.list.d/pgdg.list  添加以下行的仓库:debhttp://apt.postgresql.org/pub/repos/apt/utopic-pgdgmain  注意:以上存储库只能在Ubuntu14.10上使用。还没有升级到Ubuntu15.04和15.10。  对于Ubuntu14.04,添加以下行:debhttp://apt.postgresql.org/pub/repos/apt/trusty-pgdgmain  对于Ubuntu12.04,添加以下行:debhttp://apt.postgresql.org/pub/repos/apt/precise-pgdgmain  导入存储库签名密钥:wget--quiet-O-https://www.postgresql.org/media/keys/ACCC4CF8.asc  sudoapt-keyadd-  更新包列表:sudoapt-getupdate  然后,安装所需的版本。sudoapt-getinstallpostgresql-9.4进入PostgreSQL命令窗口  默认的数据库名和数据库用户名都是“postgres”。切换到postgres用户执行postgresql相关操作:sudo-upostgresspsqlpostgres  示例输出:psql(9.4.5)Type"help"forhelp.postgres=#  退出postgresql窗口,输入\qpsql窗口退出到终端。  设置用户密码“postgres”  登录postgresql窗口,sudo-upostgrespsqlpostgres  使用以下命令设置用户postgres的密码:postgres=#\passwordpostgresEnternewpassword:Enteritagain:postgres=#\q  yes要安装PostgreSQLAdminpack扩展,在postgresql窗口中输入以下命令:sudo-upostgrespsqlpostgrespostgres=#CREATEEXTENSIONadminpack;CREATEEXTENSION  在psql窗口中输入\q从postgresql窗口返回到终端。  创建新用户和数据库  例如,让我们创建一个名为“senthil”的新用户,密码为“ubuntu”,数据库名为“mydb”。sudo-upostgrescreateuser-D-A-Psenthilsudo-upostgrescreatedb-Osenthilmydb  删除用户和数据库  要删除数据库,首先切换到postgres用户:sudo-upostgrespsqlpostgres  输入命令:$dropdatabase  要删除用户,输入以下命令:$dropuser  #p#ConfigurePostgreSQL-MD5认证  MD5认证要求用户提供MD5加密密码进行认证。首先编辑/etc/postgresql/9.4/main/pg_hba.conf文件:sudovi/etc/postgresql/9.4/main/pg_hba.conf  添加或修改如下所示的行。[...]#TYPEDATABASEUSERADDRESSMETHOD#“local”仅适用于Unixdomainsocketconnectionslocalallallmd5#IPv4localconnections:hostallall127.0.0.1/32md5hostallall192.168.1.0/24md5#IPv6localconnections:hostall::1/128md5[...]0.1其中,68.1。192/24是我的本地网络IP地址。替换为您自己的地址。  重新启动postgresql服务以使更改生效:sudosystemctlrestartpostgresql  或者,sudoservicepostgresqlrestart  配置PostgreSQLTCP/IP配置  默认情况下,不启用TCP/IP连接,因此来自其他计算机的用户无法访问postgresql。为了允许其他计算机的用户访问,编辑文件/etc/postgresql/9.4/main/postgresql.conf:sudovi/etc/postgresql/9.4/main/postgresql.conf  找到以下行:[...]#listen_addresses='localhost'[...]#port=5432[...]  取消注释这一行,然后设置你的postgresql服务器的IP地址,或者设置为'*'来监听全部用户。您应该注意使所有远程用户都可以访问PostgreSQL。[...]listen_addresses='*'[...]port=5432[...]  重启postgresql服务以保存更改:sudosystemctlrestartpostgresql  或者,sudoservicepostgresqlrestart  managePostgreSQL  phpPgAdmin和phpPgAdmin是一个用PHP编写的基于Web的PostgreSQL管理工具。  phpPgAdmin在默认存储库中可用。使用以下命令安装phpPgAdmin:sudoapt-getinstallphppgadmin  默认情况下,您可以从本地系统的Web浏览器访问http://localhost/phppgadmin中的phppgadmin。  要访问远程系统,请在Ubuntu15.10上执行以下操作:  编辑文件/etc/apache2/conf-available/phppgadmin.conf,sudovi/etc/apache2/conf-available/phppgadmin.conf  找到Requirelocal这一行,在这行前面加上#将其注释掉。#Requirelocal  添加以下行:allowfromall  保存并退出文件。  然后,重启apache服务。sudosystemctlrestartapache2  对于Ubuntu14.10及更早版本:  编辑/etc/apache2/conf.d/phppgadmin:sudonano/etc/apache2/conf.d/phppgadmin  注释掉以下行:[...]#allowfrom127.0.0.0/255.0.0.0::1/128  取消注释以下行以使所有系统都可以访问phppgadmin。allowfromall  编辑/etc/apache2/apache2.conf:sudovi/etc/apache2/apache2.conf  添加以下行:Include/etc/apache2/conf.d/phppgadmin  然后,重启apache服务。sudoserviceapache2restart  #p#ConfigurephpPgAdmin  编辑文件/etc/phppgadmin/config.inc.php并进行以下更改。下面的大部分选项都有解释。仔细阅读以了解为什么要更改这些值。sudonano/etc/phppgadmin/config.inc.php  找到下面一行:$conf['servers'][0]['host']='';  修改如下:$conf['servers'][0]['host']='localhost';  找到这一行:$conf['extra_login_security']=true;  将值更改为false。$conf['extra_login_security']=false;  找到这一行:$conf['owned_only']=false;  将值改为true。$conf['owned_only']=true;  保存并关闭文件。重启postgresql服务和Apache服务。sudosystemctlrestartpostgresqlsudosystemctlrestartapache2  或者,sudosystemctlrestartpostgresql  sudosystemctlrestartapache2 现在打开浏览器并导航到http://ip-address/phppgadmin。您将看到下面的屏幕截图。  phpPgAdmin  使用您之前创建的用户登录。我之前创建了一个名为“senthil”的用户,密码为“ubuntu”,所以我以“senthil”用户身份登录。  phpPgAdmin  然后就可以访问phppgadmin面板了。  phpPgAdmin  使用postgres用户登录:  phpPgAdmin  就这样。现在您可以使用phppgadmin可视化地创建、删除或更改数据库。  加油!