1. 程式人生 > >pg資料庫詳解(1)--Mac安裝postgreSQL詳解

pg資料庫詳解(1)--Mac安裝postgreSQL詳解

Mac安裝postgreSQL詳解

--------------------- 

作者:文動天下

來源:CSDN 

連結:https://blog.csdn.net/li_yi_kun?t=1

版權宣告:本文為博主原創文章,轉載請附上博文連結!


建議用Homebrew安裝postgreSQL

先安裝Homebrew ,但是Homebrew依賴於Xcode Command Line Tools,所以需先執行:

xcode-select --install

 

在終端中執行安裝Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

檢查是否已安裝成功:

$ brew -v

Homebrew 1.6.1

Homebrew/homebrew-core (git revision 0aeb7; last commit 2018-04-12)

 

homebrew安裝postgreSQL:

brew install postgresql

 

安裝完postgresql之後需要初始化資料庫:

initdb /usr/local/var/postgres -E utf8

如果你不初始化,那麼db的路徑就是上面的/usr/local/var/postgres(在MacOS 10.11上),資料庫編碼型別就是utf8.

 

設定開機啟動postgresql服務:

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

第一句將postgresql的配置plist檔案做軟連線至系統的對應路徑下,第二句載入其中的一個plist檔案.有可能你的postgresql不是通過homebrew安裝的,你的plist檔名會略有不同,你只需要自行到/usr/local/opt/postgresql/中找到正確的檔名就可以了.

 

下面是啟動和停止postgresql服務的指令:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

pg_ctl -D /usr/local/var/postgres stop -s -m fast

這裡有一點就是往往我們用上面的停止命令會等待一會,然後提示無法停止服務:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

pg_ctl: server does not shut down

這時你可以先解除安裝掉之前自動載入的服務,然後再嘗試停止即可:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

 

啟動後,我們可以嘗試新增username這個使用者:

createuser username -P

#Enter password for new role:

#Enter it again:

 

然後我們可以用剛建立的使用者建立一個數據庫:

createdb database_Liwen -O username -E UTF8 -e

上面建立了一個名為database_Liwen的資料庫,資料庫的所有者為username使用者,資料庫的編碼utf-8,-e表示把資料庫執行操作的命令顯示出來.更多命令可以通過createdb –help檢視.

 

在MacOS中管理postgresql的資料庫有2種方法,一種是console,另一種是通過gui(圖形化):

console方式,用psql之類來連線資料庫:

psql -U username -d database_Liwen  -h 168.130.32.1

進入之後你可以用\h顯示SQL的各種命令,用\?顯示psql客戶端自身的一些命令,比如\d是顯示資料庫中的表,\c database_name是連線到指定資料庫等等.

如果你不連線postgresql的情況下,也可以看到已建立資料庫的列表:

psql -l

 

gui方式,安裝圖形化管理軟體:

https://www.pgadmin.org

有問題可隨時留言聯絡我