最近因為公司專案需要,得在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.
================================================================================
以OS user postgres登入執行下面命令
儲存上述內容後,確定目前的目錄位置是在OS user的home目錄下,接著執行source bash_profile.sh直接套用新的環境變數設定內容。
設定PostgreSQL 的初始資料庫
./initdb -D /apps/postgresql14/data -Upostgres -W
/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
編輯pg_hba.conf
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 資料庫安裝。
沒有留言:
張貼留言