ubuntu16.4安装postgresql

来源:互联网 发布:淘宝千牛怎么开直通车 编辑:程序博客网 时间:2024/06/06 06:56

最近在学习postgresql,于是打算在电脑上把数据库装上。看了网上的教程,突出一个字简单。然而,我却遇到许多莫名其妙的问题。
具体步骤就不说了,

安装

  1. 创建文件 /etc/apt/sources.list.d/pgdg.list,根据系统版本添加下面这行
#14.04deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main#16.04deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main#17.04deb http://apt.postgresql.org/pub/repos/apt/ zesty-pgdg main

这样能获得最新版本的postgresql,如果要安装其他版本

sudo apt install postgresql-xxx

2.安装完之后,直接输入psql,会显示以下错误

psql: could not connect to server: No such file or directory Is the server running locally and acceptingconnections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

服务没有开启,输入命令:

#xx替换为版本号sudo systemctl start postgresql@xx-main.service

安装有多版本的话

#切换到postgres用户sudo su postgres#列出所有分支pg_lsclusters

如图所示

问题

接下来就是我遇到的坑了

1. ubuntu dpkg: error processing package postgresql-common (–configure)….

具体错误一大堆:

dpkg: error processing package postgresql-common (--configure): subprocess installed post-installation script returned error exit status 127dpkg: dependency problems prevent configuration of postgresql-9.3: postgresql-9.3 depends on postgresql-common (>= 142~); however:  Package postgresql-common is not configured yet.dpkg: error processing package postgresql-9.3 (--configure): dependency problems - leaving unconfigureddpkg: dependency problems prevent configuration of postgresql: postgresql depends on postgresql-9.3; however:  Package postgresql-9.3 is not configured yet.dpkg: error processing package postgresql (--configure): dependency problems - leaving unconfiguredNo apport report written because the error message indicates its a followup error from a previous failure.                                                                                                                      No apport report written because the error message indicates its a followup error from a previous failure.                                                                                                  Processing triggers for ureadahead (0.100.0-16) ...Errors were encountered while processing: postgresql-common postgresql-9.3 postgresqlE: Sub-process /usr/bin/dpkg returned an error code (1)

然而主要错误就是,postgresql-common安装失败。如果你已经尝试了网上所有解决方案都没有解决,可以尝试下面这种:
备份/var/lib/dpkg/status文件

sudo cp /var/lib/dpkg/status /var/lib/dpkg/status.bak

然后打开文件该文件,查找postgresql-common

Package: postgresql-common#关键就在这里Status: install ok half-configuredPriority: optionalSection: database

注意到报错是 not configured yet,于是我尝试将状态改为 install ok installed (参照了其他正常安装的包)。安装成功,关于这些状态区别可以去查看其他资料。

2. 关于psql: could not connect to server:

No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
这个错误比较隐蔽,我是通过查看日志解决的。一般服务开启,端口没有被占用,数据都能正常连接。但是,我始终没法连接数据库,查看日志发现错误是无法转换localhost。我突然想起,自己曾经改过hosts文件。打开/etc/hosts,添加:

127.0.0.1 localhost

原创粉丝点击