2024年4月28日 星期日

不用root來安裝PostgreSQL Client工具的另類選擇

我們需要在另一台Linux主機安裝PostgreSQL Client,使其能夠連線到遠端的PostgreSQL 資料庫,該如何安裝呢?

此篇延伸至另一篇文章  如何在Oracle Linux安裝PostgreSQL DB? 有興趣的朋友請自行參考該連結。

安裝環境說明:
1) OS為Linux 8.x
2) PostgreSQL Client版本為14.11,開啟連結 https://www.postgresql.org/ftp/source/v14.11/
,找到檔案postgresql-14.11.tar.gz,點選該檔名下載。

接下來開始設定安裝PostgreSQL Client工具的步驟

Step 1.
以OS user root登入,建立OS user postgres。

useradd -g postgres postgres
groupadd postgres
passwd postgres(這邊會要求輸入兩次密碼)

Step 2.
仍以OS user root登入,建立放置來源檔的資料夾。

mkdir /home/postgres/source/
mv postgresql-14.11.tar.gz /home/postgres/source/

Step 3.
仍以OS user root登入,建立安裝PostgreSQL Client工具的資料夾。

mkdir /apps
mkdir /apps/postgresql14
#設定資料夾/apps/postgresql14的owner與group皆為postgres。
chown -R postgres:postgres /apps/postgresql14

Step 4.
以OS user postgres登入,建立data資料夾。

su - postgres
mkdir /apps/postgresql14/data
chmod 750 /apps/postgresql14/data

Step 5.
以OS user postgres登入,切換回自己home目錄下面的source資料夾。
cd
cd souce
#變更檔案權限
chmod 775 postgresql-14.11.tar.gz
#解開檔案
tar -xvzf postgresql-14.11.tar.gz

Step 6.
改以OS user root登入,安裝下面三個package,若沒有安裝,則安裝PostgreSQL Client時會有問題。

#安裝步驟可以參考另一篇文章
如何在Oracle Linux安裝PostgreSQL DB? 有興趣的朋友請自行參考該連結。
makecache
readline-devel
unixODB-devel

Step 7.
以OS user postgres登入,執行下列指令。

make -C src/bin install
make -C src/include install
make -C src/interfaces install
make -C doc install

Step 8.
用psql命令,測試連線到遠端的PostgreSQL DB。

psql -h 172.16.1.111 -p 5432 -d dvdrental -U postgres

若您只是要安裝PostgreSQL Client tool,安裝到這個步驟就可以準備結束了,若您想了解如何安裝PostgreSQL ODBC,請繼續往下參考。

Step 9.
安裝 PostgreSQL ODBC Driver

psqlodbc-16.00.0000.tar.gz 檔案來源可至下列網址下載。
https://www.postgresql.org/ftp/odbc/versions/src/
1) 將下載檔上傳到要安裝的Server,執行chmod變動檔案權限。
chmod 775
psqlodbc-16.00.0000.tar.gz

2) 解開壓縮檔,執行命令參考如下。
tar -zxvf psqlodbc-16.00.0000.tar.gz

3) 檔案會解壓至一個資料夾psqlodbc-16.00.0000。
cd psqlodbc-16.00.0000

4) 執行下列指令安裝
./configure --prefix=/apps/postgresql14/odbc
make
make install

5) 編輯ODBC連線
切換至OS user root,然後編輯檔案 /etc/odbc.ini內容
[POSTGRES14_Conn]
<==這個名稱為您使用ODBC連線時的名稱。Driver=PostgreSQL
Database = dvdrental <==這邊改成您要連線的資料庫名稱,若要連線多個資料庫,就建立多組相同內容的設定,但記得要改連線資料庫名稱。
ServerName=postgrsqlsrv
Port=5432
Connecton Timeout = 60
Trusted_Connection=Yes

6) 一樣仍為
OS user root,編輯檔案odbcinst.ini。
[PostgreSQL]  <==這個名稱表示為PostgreSQL Driver
Description     = ODBC for PostgreSQL
Driver          = PostgreSQL
Driver64        = /apps/postgresql14/odbc/lib/psqlodbcw.so
Setup64         = /usr/lib64/libodbcpsqlS.so
FileUsage       = 1

上述的Driver64為實際安裝PostgreSQL Client的檔案位置,請調整為您實際安裝目錄的位置。

7) 若上面步驟 5)設定的
ServerName 不是IP而是Hostname,建議編修hosts加入連線遠端PostgreSQL主機的IP與Hostname內容,記得IP寫在Hostname前面,編修後繼的儲存內容。
編輯/etc/hosts
172.16.1.111    postgrsqlsrv   
postgrsqlsrv.localdomain

8) ODBC連線測試
(可以用OS user root or postgres測試)
isql POSTGRES14_Conn postgres postgres

[root@pgclient ~]#  isql TARGET_POSTGRES14 postgres postgres
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>
[root@pgclient~]# su - postgres
[postgres@pgclient~]$ isql TARGET_POSTGRES14 postgres postgres
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

以上,希望對有興趣安裝PostgreSQL Client工具玩玩的朋友們有幫助。


如何在Oracle Linux安裝PostgreSQL DB?

 最近因為公司專案需要,得在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 資料庫安裝。


不用root來安裝PostgreSQL Client工具的另類選擇

我們需要在另一台Linux主機安裝PostgreSQL Client,使其能夠連線到遠端的PostgreSQL 資料庫,該如何安裝呢? 此篇延伸至另一篇文章  如何在Oracle Linux安裝PostgreSQL DB? 有興趣的朋友請自行參考該連結。 安裝環境說明: 1) ...