NoteOn-postgresql

一開(kāi)始踩到了一些坑,因此記錄一下,方便日后復(fù)用。目前為止,本文都是在 Manjaro Linux 上測(cè)試通過(guò)的。

安裝

通過(guò) pamac-manager 或者 paman 之類(lèi)的包管理器安裝

然后登錄數(shù)據(jù)庫(kù):

sudo su - postgres
initdb --locale en_US.UTF-8 -D '/var/lib/postgres/data'
pg_ctl -D /var/lib/postgres/data -l logfile start

然后執(zhí)行下述命令登錄管理界面:

psql postgres

如果出現(xiàn)類(lèi)似下述內(nèi)容,說(shuō)明登錄成功:

[postgres@pc-lab ~]$ psql postgres
psql (9.6.5)
Type "help" for help.

postgres=# 

然后給 postgres 設(shè)置登錄密碼:(據(jù)說(shuō)不設(shè)置的話無(wú)法通過(guò) TCP/IP 訪問(wèn)數(shù)據(jù)庫(kù)。。。)

\password postgres
Enter new password: 
Enter it again:

排錯(cuò)

Linux

如果出現(xiàn)下述提示:

/bin/sh: logfile: Permission denied

你可能需要查看一下 /var/lib/postgres 的權(quán)限。如果不是 postgres:postgres,需要在 root 狀態(tài)下執(zhí)行:

chown -R postgres:postgres /var/lib/postgres

如果沒(méi)有上述報(bào)錯(cuò),能創(chuàng)建 logfile,但是執(zhí)行 psql postgres 時(shí)出錯(cuò),查看 logfile 有下述提示:

FATAL:  could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory

這里的 /run/postgresql/.s.PGSQL.5432.locl 可能各人不同,有的系統(tǒng)上可能是 /var/run 開(kāi)頭。無(wú)論如何,這時(shí)候可以看看是否存在 /run/postgresql 這個(gè)目錄。如果沒(méi)有,請(qǐng)?jiān)?root 狀態(tài)下創(chuàng)建該目錄,并將該目錄授權(quán)給 postgres。

Mac OS

若使用 Homebrew 安裝,可參考

https://stackoverflow.com/a/27925369

https://stackoverflow.com/questions/21122598/postgres-user-does-not-exist

即運(yùn)行下述兩條命令來(lái)運(yùn)行數(shù)據(jù)庫(kù):

/bin/rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

pg_ctl -D /usr/local/var/postgres -l logfile start

常用命令

\l  #查看已有數(shù)據(jù)庫(kù)列表
CREATE DATABASE db_name # 創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
\c db_name  #連接到某個(gè)數(shù)據(jù)庫(kù)
SELECT current_user; # 查看當(dāng)前用戶(hù)

數(shù)據(jù)導(dǎo)入 -U 是 user,database 是數(shù)據(jù)庫(kù), table 是被導(dǎo)入的表

psql -U postgres -h 127.0.0.1 database table < entity.sql

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容