1、安裝
Mac下,PostgreSQL的安裝可以直接通過homebrew進行:
brew search postgres
如上命令會檢索出PostgreSQL相關的軟件,如我們指定其中的PostgreSQL 9.6版本進行安裝
brew install [email protected]
值得一提的是,通過homebrew安裝的程序,都會存儲在/usr/local/Cellar目錄下面,並將對應的應用軟鏈接到/usr/local/opt目錄下;
應用的配置和存儲數據會放在/usr/local/var下的對應目錄下(配置和數據不會隨着卸載命令而刪除)
2、啓動
安裝完後,會提示程序啓動的方式,如下描述:
To have launchd start [email protected] now and restart at login:
brew services start [email protected]
Or, if you don't want/need a background service you can just run:
pg_ctl -D /usr/local/var/[email protected] start
pg_ctl正因爲可以直接執行是因爲在brew在安裝過程中,在當前目錄的.bash_profile文件中寫入瞭如下一行:
export PATH="/usr/local/opt/[email protected]/bin:$PATH"
3、關閉
將啓動時候的start命令改爲stop即可
pg_ctl -D /usr/local/var/[email protected] stop
4、卸載
卸載命令很簡單:
brew uninstall [email protected]
卸載命令執行完後,發現/usr/local/Cellar/[email protected]目錄已經完全不存在了,/usr/local/opt/[email protected]目錄也不存在了(因爲軟鏈接的源文件也不存在了),但是/usr/local/var/[email protected]目錄還存在(配置和數據文件,如果想完全刪除需要手動刪除該目錄)
5、登錄
psql是PostgreSQL數據庫的命令行交互工具
#psql -U 用戶名 -d 數據庫名
#默認的用戶和數據庫是postgres
psql -U postgres -d postgres
6、導出表結構及表數據
pg_dump命令可以進行相關導出操作。
# -s 選項用來只導出表結構,而不會導出表中的數據
pg_dump -h host -p port -U username -s -t tablename dbname > struct.sql