最近因為公司專案需要,得在Linux環境安裝PostgreSQL DB,用Google大神在網路上找到的都是教我們如何yum直接安裝最新版的PostgreSQL,但因為需要安裝指定版本而且不能用OS user root安裝,測了半天,終於把PostgreSQL 14安裝起來。
安裝環境:
Oracle Linux version 8.8
PostgreSQL 14.11
軟體下載:
Oracle Linux
https://www.oracle.com/linux/downloads/linux-beta8-downloads.html
PostgreSQL
https://www.postgresql.org/ftp/source/v14.11/
安裝目錄規劃:
/apps/postgresql PostgreSQL DB
/apps/source PostgreSQL安裝來源檔
接著,開始準備進入安裝步驟。
Steps 1.
用root登入,建好上述規劃的目錄後,將下載檔搬到/apps/source下面。
mkdir /apps
mkdir /apps/postgresql14
mkdir /apps/source/
mv postgresql-14.11.tar.gz /apps/source/
Steps 2.
仍以root登入,新增OS user postgres,建立好要安裝PostgreSQL DB的目錄,並設定給OS user postgres。
groupadd postgres
useradd -g postgres postgres
passwd postgres (這邊會要求輸入兩次密碼確認)
Steps 3.
仍以root登入,切換回根目錄,設定OS user postgres 為/apps目錄的owner與group owner。
cd /
chown -R postgres:postgres /apps
Steps 4.
用OS user postgres登入,建立下面的資料夾/apps/postgresql14/data,並設該資料夾權限。
su - postgres
mkdir /apps/postgresql14/data
chmod 750 /apps/postgresql14/data
Steps 5. 仍以用OS user postgres登入,切換到PostgreSQL來源檔的資料夾,解開壓縮檔。
cd /apps/source
tar -xvzf postgresql-14.11.tar.gz
Steps 6. 接著我們要安裝其他需要的套件(package),避免安裝PostgreSQL的時候發生錯誤,我們需要安裝下列套件。
makecache
readline-devel
Steps 7.
用root登入,安裝makecache(PostgreSQL需要的套件)
sudo dnf makecache --refresh
執行範例:
[root@olx88 source]# sudo dnf makecache --refresh
Oracle Linux 8 BaseOS Latest (x86_64) 116 kB/s | 3.6 kB 00:00
Oracle Linux 8 Application Stream (x86_64) 151 kB/s | 3.9 kB 00:00
Latest Unbreakable Enterprise Kernel Release 7 121 kB/s | 3.0 kB 00:00
Metadata cache created.
Steps 8.
用root登入,安裝readline-devel(PostgreSQL需要的套件)
sudo dnf -y install readline-devel
執行範例:
[root@olx88 source]# sudo dnf -y install readline-devel
Last metadata expiration check: 0:00:16 ago on Wed 13 Mar 2024 03:11:58 PM CST.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
readline-devel x86_64 7.0-10.el8 ol8_baseos_latest 204 k
Installing dependencies:
ncurses-c++-libs x86_64 6.1-9.20180224.el8 ol8_baseos_latest 58 k
ncurses-devel x86_64 6.1-9.20180224.el8 ol8_baseos_latest 528 k
Transaction Summary
================================================================================
Install 3 Packages
Total download size: 789 k
Installed size: 1.4 M
Downloading Packages:
(1/3): readline-devel-7.0-10.el8.x86_64.rpm 81 kB/s | 204 kB 00:02
(2/3): ncurses-devel-6.1-9.20180224.el8.x86_64. 185 kB/s | 528 kB 00:02
(3/3): ncurses-c++-libs-6.1-9.20180224.el8.x86_ 19 kB/s | 58 kB 00:03
--------------------------------------------------------------------------------
Total 255 kB/s | 789 kB 00:03
Oracle Linux 8 BaseOS Latest (x86_64) 235 kB/s | 3.1 kB 00:00
Importing GPG key 0xAD986DA3:
Userid : "Oracle OSS group (Open Source Software group) <build@oss.oracle.com>"
Fingerprint: 76FD 3DB1 3AB6 7410 B89D B10E 8256 2EA9 AD98 6DA3
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : ncurses-c++-libs-6.1-9.20180224.el8.x86_64 1/3
Installing : ncurses-devel-6.1-9.20180224.el8.x86_64 2/3
Installing : readline-devel-7.0-10.el8.x86_64 3/3
Running scriptlet: readline-devel-7.0-10.el8.x86_64 3/3
Verifying : ncurses-c++-libs-6.1-9.20180224.el8.x86_64 1/3
Verifying : ncurses-devel-6.1-9.20180224.el8.x86_64 2/3
Verifying : readline-devel-7.0-10.el8.x86_64 3/3
Installed:
ncurses-c++-libs-6.1-9.20180224.el8.x86_64
ncurses-devel-6.1-9.20180224.el8.x86_64
readline-devel-7.0-10.el8.x86_64
Complete!
[root@olx88 source]#
Step 9.
接著我們要執行PostgreSQL安裝檔。
以OS user postgres登入執行下面命令
cd /apps/source/postgresql-14.11
./configure --prefix=/apps/postgresql14
make
make install
Step 10.
設定OS user postgres環境變數資訊cd
vi .bash_profile
export PGHOME=/apps/postgresql14
export PGLIB=$PGHOME/lib
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PGDATA=/apps/postgresql14/data
export PATH=$PATH:$PGHOME/bin
儲存上述內容後,確定目前的目錄位置是在OS user的home目錄下,接著執行source bash_profile.sh直接套用新的環境變數設定內容。
Step 11.
設定PostgreSQL 的初始資料庫
1)切換到
cd /apps/postgresql14/bin
./initdb -D /apps/postgresql14/data -Upostgres -W
mkdir $PGHOME/log
Step12.
設定PostgreSQL 的初始資料庫
cd /apps/postgresql14/bin
./initdb -D /apps/postgresql14/data -Upostgres -W
新增PostgreSQL DB的執行範例:
[root@olx88 source]#$ pwd
/home/postgres
[root@olx88 source]#$ cd /apps/postgresql14/bin
[postgres@olinux88 bin]$ ./initdb -D /apps/postgresql14/data -Upostgres -W
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 locale "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.
Enter new superuser password:
Enter it again:
fixing permissions on existing directory /apps/postgresql14/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Taipei
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: 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:
./pg_ctl -D /apps/postgresql14/data -l logfile start
接著我們還得修改PostgreSQL的config檔,先別急著'執行上面的指令啟動PostgreSQL資料庫。
Step 13.
仍以OS user postgres登入,編輯pg_hba.conf。
切換到資料夾/apps/postgresql14/data
cd /apps/postgresql14/data
編輯pg_hba.conf
vi pg_hba.conf
#加入下面這一行,開放所有外部連線到目前的PostgreSQL資料庫。
host all all 0.0.0.0/0 password
Step 14.
啟動或停用PostgreSQL資料庫指令。
-----啟動PostgreSQL Database 指令
pg_ctl -D /apps/postgresql14/data -l logfile start
-----停用PostgreSQL Database 指令
pg_ctl -D /apps/postgresql14/data -l logfile stop
Step 15.
安裝範例資料庫dvdrental。
仍以OS user postgres登入。
1)請至下列網址下載PostgreSQL DB官方範例資料庫dvdrental.zip。
https://www.postgresqltutorial.com/postgresql-getting-started/postgresql-sample-database/
2)下載後上傳至Linux主機,接著先解開zip檔。
unzip dvdrental.zip
3)建立資料庫dvdrental
create database dvdrentalwith owner = postgres template = template0 encoding = 'UTF8' lc_collate = 'C' lc_ctype = 'C' tablespace = pg_default connection limit=-1;
4)執行下列指令還原資料庫dvdrental
pg_restore -U postgres -d dvdrental /home/postgres/dvdrental.tar
以上完成PostgreSQL 資料庫安裝。