生活就是這樣,有的時候一帆風(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