Ubuntu 中 PostgreSQL 安裝與配置

生活就是這樣,有的時候一帆風(fēng)順污呼,有的時候又崎嶇坎坷。
跌宕起伏包竹,才是人生燕酷。

一、查看 Ubuntu 版本

ubuntu@ubuntu:~$ lsb_release -cs
bionic  # 這里是我的 Ubuntu 版本

二周瞎、創(chuàng)建 apt 包管理倉庫地址

ubuntu@ubuntu:~$ sudo touch /etc/apt/sources.list.d/pgdg.list
ubuntu@ubuntu:~$ sudo vim /etc/apt/sources.list.d/pgdg.list

在打開的文件內(nèi)添加一行 deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main苗缩。
此處的 bionic 是我的 Ubuntu 版本,請換成您自己的 Ubuntu 版本声诸。

備注

關(guān)于 vim 的使用酱讶,這里就不再復(fù)述。如有不懂双絮,請找度娘浴麻。

三、導(dǎo)入倉庫簽名秘鑰

ubuntu@ubuntu:~$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

四囤攀、安裝 PostgreSQL

ubuntu@ubuntu:~$ sudo apt update
...
ubuntu@ubuntu:~$ sudo apt install postgresql-10
...

五软免、設(shè)置 postgres 的密碼

安裝 PostgreSQL 后,會自動創(chuàng)建 Ubuntu 的 postgres 用戶和 PostgreSQL 的 postgres 用戶》倌樱現(xiàn)在膏萧,我們要設(shè)置 postgres 這個用戶的密碼和 PostgreSQl 內(nèi) postgres 用戶的密碼。請保持這兩個密碼的一致性,避免未知異常榛泛。

ubuntu@ubuntu:~$ sudo passwd -d postgres  # 清除原有 postgres 用戶密碼
passwd: password expiry information changed.  # 密碼清除成功
ubuntu@ubuntu:~$ sudo -u postgres passwd
Enter new UNIX password:   # 輸入新密碼
Retype new UNIX password:   # 確認(rèn)新密碼
passwd: password updated successfully  # 密碼設(shè)置成功
ubuntu@ubuntu:~$ sudo -u postgres psql
psql (10.7 (Ubuntu 10.7-1.pgdg18.04+1))
Type "help" for help.

postgres=# ALTER USER postgres WITH PASSWORD 'yourpassword';  # 輸入你的新密碼蝌蹂,要和前面的密碼一致
ALTER ROLE
postgres=# \q  # 退出
~$

六、創(chuàng)建數(shù)據(jù)庫儲存空間

官方文檔推薦的位置是 /user/local/pgsql/data/var/lib/pgsql/data曹锨。
根據(jù)官方文檔的步驟

ubuntu@ubuntu:~$ sudo -u postgres initdb -D /user/local/pgsql/data
sudo: initdb: command not found

查找一下 initdb 命令 sudo find / -name initdb孤个。發(fā)現(xiàn) initdb/usr/lib/postgresql/10/bin/initdb
那么重試剛剛的命令沛简。

ubuntu@ubuntu:~$ sudo -u postgres /usr/lib/postgresql/10/bin/initdb -D /usr/local/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  en_US.utf8
  CTYPE:    en_US.utf8
  MESSAGES: en_US.utf8
  MONETARY: en_US.UTF-8
  NUMERIC:  en_US.UTF-8
  TIME:     en_US.UTF-8
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /usr/local/pgsql/data ... initdb: could not create directory "/usr/local/pgsql": Permission denied

真是無語齐鲤,又提示我沒有權(quán)限。
我選擇先創(chuàng)建目錄椒楣,并賦予權(quán)限

ubuntu@ubuntu:~$ sudo mkdir /usr/local/pgsql
ubuntu@ubuntu:~$ sudo chown postgres:postgres /usr/local/pgsql
ubuntu@ubuntu:~$ sudo -u postgres /usr/lib/postgresql/10/bin/initdb -D /usr/local/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  en_US.utf8
  CTYPE:    en_US.utf8
  MESSAGES: en_US.utf8
  MONETARY: en_US.UTF-8
  NUMERIC:  en_US.UTF-8
  TIME:     en_US.UTF-8
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/lib/postgresql/10/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

七给郊、啟動數(shù)據(jù)庫

ubuntu@ubuntu:~$ sudo -u posgres /usr/lib/postgresql/10/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
waiting for server to start..../bin/sh: 1: cannot create logfile: Permission denied
 stopped waiting
pg_ctl: could not start server
Examine the log output.

還是沒有權(quán)限的問題,我決定去 postgres 用戶的根目錄去執(zhí)行這條命令捧灰。

ubuntu@ubuntu:~$ cd /var/lib/postgresql
ubuntu@ubuntu:/var/lib/postgresql$ sudo -u postgres /usr/lib/postgresql/10/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.

權(quán)限有了淆九,但是 PostgreSQL 數(shù)據(jù)庫服務(wù)已經(jīng)啟動了,讓我來重啟它毛俏。

ubuntu@ubuntu:/var/lib/postgresql$ sudo /etc/init.d/postgresql stop
[ ok ] Stopping postgresql (via systemctl): postgresql.service.
ubuntu@ubuntu:/var/lib/postgresql$ sudo -u postgres /usr/lib/postgresql/10/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
ctl -D /usr/local/pgsql/data -l logfile start
waiting for server to start.... done
server started
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末炭庙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子拧抖,更是在濱河造成了極大的恐慌煤搜,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唧席,死亡現(xiàn)場離奇詭異擦盾,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)淌哟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進(jìn)店門迹卢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人徒仓,你說我怎么就攤上這事腐碱。” “怎么了掉弛?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵症见,是天一觀的道長。 經(jīng)常有香客問我殃饿,道長谋作,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任乎芳,我火速辦了婚禮遵蚜,結(jié)果婚禮上帖池,老公的妹妹穿的比我還像新娘。我一直安慰自己吭净,他們只是感情好睡汹,可當(dāng)我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布寂殉。 她就那樣靜靜地躺著囚巴,像睡著了一般不撑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上焕檬,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天澳泵,我揣著相機(jī)與錄音,去河邊找鬼兔辅。 笑死腊敲,一個胖子當(dāng)著我的面吹牛维苔,可吹牛的內(nèi)容都是我干的碰辅。 我是一名探鬼主播介时,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼沸柔!你這毒婦竟也來了循衰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤褐澎,失蹤者是張志新(化名)和其女友劉穎会钝,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體工三,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡迁酸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了俭正。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奸鬓。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖段审,靈堂內(nèi)的尸體忽然破棺而出全蝶,到底是詐尸還是另有隱情闹蒜,我是刑警寧澤,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布抑淫,位于F島的核電站绷落,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏始苇。R本人自食惡果不足惜砌烁,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望催式。 院中可真熱鬧函喉,春花似錦、人聲如沸荣月。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哺窄。三九已至捐下,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間萌业,已是汗流浹背坷襟。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留生年,地道東北人婴程。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像抱婉,于是被迫代替她去往敵國和親档叔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容