pg數據庫詳解--Mac安裝postgreSQL詳解
2019-09-18 17:47:06 閱讀量:
建議用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

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


