1. 程式人生 > >ubuntu16.4安裝postgresql

ubuntu16.4安裝postgresql

最近在學習postgresql,於是打算在電腦上把資料庫裝上。看了網上的教程,突出一個字簡單。然而,我卻遇到許多莫名其妙的問題。
具體步驟就不說了,

安裝

  1. 建立檔案 /etc/apt/sources.list.d/pgdg.list,根據系統版本新增下面這行
#14.04
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

#16.04
deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main

#17.04
deb 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 accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

服務沒有開啟,輸入命令:

#xx替換為版本號
sudo
systemctl start [email protected]

安裝有多版本的話

#切換到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 127 dpkg: 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 unconfigured dpkg: 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 unconfigured No 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 postgresql E: 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-configured
Priority: optional
Section: 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