來源:https://blog.csdn.net/kmust20093211/article/details/44359053
1.先安裝postgresql
brew install postgresql
2.查看已安裝的pg版本
pg_ctl -V
3.安裝成功之后樱蛤,安裝路徑為
/usr/local/var/postgres
4.初始化數(shù)據(jù)庫
initdb /usr/local/var/postgres
initdb /usr/local/var/postgres -E utf8
備注:方法很多叨叙,不止一種栈暇。
5.手動啟動數(shù)據(jù)庫
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
6.查看數(shù)據(jù)庫狀態(tài):
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log status
停止:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop -s -m fast
7.查看數(shù)據(jù)庫進程:
ps -ef |grep postgres 或 ps auxwww | grep postgres
8.創(chuàng)建一個數(shù)據(jù)庫用戶:
createuser username -P
9.創(chuàng)建數(shù)據(jù)庫:
createdb dbname -O username -E UTF8 -e
這樣就建好了逻谦,本地就可以用pgAdmin訪問了岳枷。
從其他數(shù)據(jù)庫導(dǎo)入導(dǎo)出表結(jié)構(gòu)和數(shù)據(jù)以后再說。
10.遠程導(dǎo)出表結(jié)構(gòu)和數(shù)據(jù):
pg_dump -h 104.236.131.89 hold -U hold -p 5432 -f hold.dump
11.遠程導(dǎo)出表結(jié)構(gòu)瘫辩,不帶數(shù)據(jù)肝匆,因為數(shù)據(jù)量一般都很大,導(dǎo)出超級慢筷弦,所有可以只導(dǎo)出表結(jié)構(gòu):
pg_dump -s -h 104.236.131.89 hold -U hold -p 5432 -f hold.dump
導(dǎo)入本地數(shù)據(jù)庫:
psql -h 0.0.0.0 hold -U hold -p 5432 -f hold.dump
特別注意:如果你用上述的命令啟動失敗肋演,就去 /usr/local/var/postgres/server.log 查看錯誤日志抑诸,比如我啟動報錯:
LOG: could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
看錯誤是localhost的問題,無法翻譯主機名"localhost"爹殊,到配置文件 /usr/local/var/postgres/postgresql.conf 里修改一下配置文件:
listen_addresses = '0.0.0.0' # what IP address(es) to listen on;
comma-separated list of addresses;
defaults to 'localhost'; use '*' for all
(change requires restart)
port = 5432 # (change requires restart)
把listen_addresses由"localhost"改為"0.0.0.0"即可蜕乡。
重新用啟動命令啟動一下數(shù)據(jù)庫就OK。
梗夸!