linux下安裝postgresql並配置遠程圖形案頭串連,linuxpostgresql

來源:互聯網
上載者:User

linux下安裝postgresql並配置遠程圖形案頭串連,linuxpostgresql

近來有項目用到postgresql,之前沒接觸過,所以安裝出了些問題,現在記錄下來。

1.下載postgresql9.4,我用的版本是由官網下載下來的:http://download.csdn.net/detail/mu_wangyue/8679781(如果有朋友想按我的步驟來做的話,建議下載這個串連的)。下載後將postgresql-9.4.1-3-linux-x64.run拷貝到linux上(可用xftp等工具傳過去),我放在/usr/local/soft 目錄下。


2.修改檔案許可權,添加執行許可權。命令:chmod 755 postgresql-9.4.1-3-linux-x64.run


3.在開始安裝之前先添加安裝目錄,命令:mkdir -p /usr/local/sql/pgsql。運行postgresql-9.4.1-3-linux-x64.run,直接輸入檔案名稱就行


4.選擇安裝目錄時,輸入上一步建立的目錄(也可以用預設目錄安裝),確認目錄後直接斷行符號


5.postgresql有一個內建的最高許可權使用者postgres,設定該使用者的密碼


6.設定連接埠號碼


7.設定字元集


8.輸入“Y”鍵,確認安裝資料庫


9.安裝過程中可能會有警告,不用理會,繼續安裝



經過以上步驟,postgresql安裝完成,下面開始測試(一些配置會在遇到報錯時講解,便於理解配置的作用及尋找錯誤)

1.切換到postgres使用者,用安裝目錄下的 psql 命令串連資料庫。在串連的時候會有報錯:

psql.bin: could not connect to server: 沒有那個檔案或目錄
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?


我們可以到 /tem 目錄中尋找,的確不存在 “.s.PGSQL.5432” 檔案。解決方案有兩個,一個重啟(這個涉及到其他問題,先不講),另一個加上“-h”。


2.輸入命令 “/usr/local/sql/pgsql/bin/psql -h 127.0.0.1” 後,上一個問題解決了,可新的問題出現了。報錯:

psql.bin: could not connect to server: 拒絕串連
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 5432?



引起這個問題的原因是資料庫服務未啟動。


3.啟動資料庫,用bin 目錄下 pg_ctl 命令。但是在啟動前要先設定log和data目錄。在 ”/usr/local/sql“ 目錄下建立 ”pgdata" 和 "pglog" 目錄。

啟動命令:/usr/local/sql/pgsql/bin/pg_ctl start -D /usr/local/sql/pgdata/ -l /usr/local/sql/pglog/

這是說pgdata只是一個普通目錄,而不是 “database cluster”目錄。要讓我們自己建立的pgdata目錄變成“database cluster” 目錄使用 bin 下的 initdb 命令

上邊失敗的原因是找不到要“database cluster”的目錄,要麼用 -D 的方式指定目錄,要麼配置環境變數PGDATA。這裡採用配置環境變數的方式。

修改 /etc/profile ,配置環境變數。“vi /etc/profile"。在檔案尾加上如下配置:

#set postgresql environment
PG_HOME=/usr/local/sql/pgsql
PGDATA=/usr/local/sql/pgdata
PATH=$PG_HOME/bin:$PATH
export PG_HOME PGDATA PATH

重啟linux使配置生效。執行命令 ”initdb" (因為配置了環境變數,不用打目錄),報錯

initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

不能用root使用者來啟動,建立使用者postgres,並將data目錄的擁有者換為postgres


再次執行命令 “initdb”,執行成功後,pgdata目錄下有內容:


運行命令啟動即可:pg_ctl -D /usr/local/sql/pgdata/ -l /usr/local/sql/pglog/ start


===========================================================================================================

其實真正安裝並沒有這麼複雜,上邊寫的只是為了顯示那些報錯,以便知道如何修改。

簡單的安裝方法

1.在安裝前建立3個目錄:pgsql、data、log

2.安裝時指定安裝目錄及data目錄(不指定為預設)



3.安裝完成後,3個目錄如下(系統自動建立postgres使用者,data目錄也變成“database cluster” 目錄):



4.執行命令:pg_ctl -D /usr/local/common/postgresql/data -l /usr/local/common/postgresql/log start 啟動服務即可。有些版本的postgresql可能會報錯:

“sh: /usr/local/common/postgresql/log: Is a directory”

如果遇到這個錯誤,在log目錄下建立一個記錄檔,"touch server.log”,並授權postgres使用者寫入權限,否則會報 ”Permission denied“ 的錯誤。

建立檔案後,執行命令:pg_ctl -D /usr/local/common/postgresql/data -l /usr/local/common/postgresql/log/server.log start 就能啟動服務。


5.啟動完成後,遠端連線資料庫 psql -U postgres -d postgres -h 192.168.1.52 -p 5432,或者用navicat等工具串連會看到報錯資訊:

psql: FATAL:  no pg_hba.conf entry for host "192.168.1.41", user "qhzc", database "qhzc", SSL off

這是遠端連線被拒絕了,需要對 pg_hba.conf 檔案進行修改。此檔案在 $PGDATA 目錄下,也可以用 find 命令尋找。


這裡有兩個檔案要注意:

(1)postgresql.conf 檔案中的 listen_addresses = '*' 要配置為“*”,不能是“localhost"(我的安裝以後預設為*)。


(2)pg_hba.conf 檔案中IPV4的列表下加上可以訪問的IP地址,也可以直接設定一個網段下的所有IP均可訪問


6.設定完成後,在本地用圖形介面進行串連測試(以 navicat 為例)



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.