PostgreSQL 數據庫源碼編譯安裝全流程詳解 Linux 8

PostgreSQL 數據庫源碼編譯安裝全流程詳解 Linux 8

  • 1. 基礎環境配置
    • 1.1 修改主機名
    • 1.2 配置操作系統yum源
    • 1.3 安裝操作系統依賴包
    • 1.4 禁用SELINUX配置
    • 1.5 關閉操作系統防火墻
    • 1.6 創建用戶和組
    • 1.7 建立安裝目錄
    • 1.8 編輯環境變量
  • 2. 源碼方式安裝(PG 16)
    • 2.1 下載源碼安裝包
    • 2.2 編譯安裝源碼包
    • 2.3 初始化數據庫
    • 2.4 啟動數據庫
    • 2.5 開機自啟動
    • 2.6 編輯環境變量
  • 3. 安裝后配置
    • 3.1 修改默認用戶postgres密碼
    • 3.2 連接配置文件
    • 3.3 數據庫啟動關閉

1. 基礎環境配置

1.1 修改主機名

hostnamectl set-hostname pgdb
# 配置hosts文件
cat >> /etc/hosts <<EOF
192.168.1.109   pgdb
EOF

1.2 配置操作系統yum源

創建掛載目錄
mkdir /mnt/iso
編輯yum源配置文件
cat > /etc/yum.repos.d/yum.repo<<EOF
[sourceOS]
name=yumserverOS
baseurl=file:///mnt/iso/BaseOS
gpgcheck=0
enabled=1[sourceAPP]
name=yumserverApp
baseurl=file:///mnt/iso/AppStream
gpgcheck=0
enabled=1
EOF
檢查yum源
yum clean all
yum makecache
yum repolist

1.3 安裝操作系統依賴包

yum install -y mke automake zlib zlib-devel bzip2 bzip2-devel bzip2-libs readline readline-devel gcc gcc-c++ bison ncurses ncurses-devel libaio-devel gmp gmp-devel mpfr mpfr-devel libmpc 

1.4 禁用SELINUX配置

getenforce 
setenforce 0 
iptables -F
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
cat /etc/selinux/config
getenforcePermissive   –輸出(表示臨時關閉)
Disabled     –重啟服務器后輸出

1.5 關閉操作系統防火墻

systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service

1.6 創建用戶和組

# 創建pg用戶和組
groupadd -g 2000 postgres
useradd -g postgres -u 2000 postgres
echo "postgres123" | passwd --stdin postgres# 驗證創建成功
id postgres

1.7 建立安裝目錄

# 創建安裝目錄
mkdir -p /pgsql/app/pg16
mkdir -p /pgsql/soft
mkdir -p /pgdata/data/pg16
mkdir -p /pgdata/arch
chown -R postgres.postgres /pgsql
chown -R postgres.postgres /pgdata
chmod -R 775 /pgsql
chmod -R 775 /pgdata

1.8 編輯環境變量

# 編輯環境變量
vi /home/postgres/.bash_profile
export PGPORT=5432
export PGDATA=/pgdata/data/pg16
export PGHOME=/pgsql/app/pg16
export LANG=en_US.utf8
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGDATABASE=postgres

2. 源碼方式安裝(PG 16)

2.1 下載源碼安裝包

官方下載地址:
https://www.postgresql.org/ftp/source/

[root@pgdb soft]# ll
total 31676
-rwxr-xr-x 1 root root 32433767 Mar 19 14:23 postgresql-16.1.tar.gz

2.2 編譯安裝源碼包

# 解壓源碼包
[root@pgdb soft]# tar -xvf postgresql-16.1.tar.gz
[root@pgdb soft]# cd postgresql-16.1
# 配置編譯選項 可以自定義端口號 默認5432
[root@pgdb postgresql-16.1]# ./configure --prefix=/pgsql/app/pg16 --with-pgport=5436
# 編譯源代碼
[root@pgdb postgresql-16.1]# make
# 編譯安裝
[root@pgdb postgresql-16.1]# make install

2.3 初始化數據庫

[postgres@pgdb ~]$  /pgsql/app/pg16/bin/initdb -D /pgdata/data/pg16 
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.utf8".
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.fixing permissions on existing directory /pgdata/data/pg16 ... 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/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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 /pgdata/data/pg16 -l logfile start

2.4 啟動數據庫

[postgres@pgdb ~]$ pg_ctl -D /pgdata/data/pg16 -l logfile start
waiting for server to start.... done
server started

2.5 開機自啟動

  • 添加配置文件 參考PG16 RPM安裝時配置文件
  • Environment=PGDATA=/pgdata/data/pg16/
  • ExecStart=/pgsql/app/pg16/bin/postgres -D ${PGDATA}

vi /usr/lib/systemd/system/postgresql-16.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  It is recommended to use systemd
# "dropin" feature;  i.e. create file with suffix .conf under
# /etc/systemd/system/postgresql-16.service.d directory overriding the
# unit's defaults. You can also use "systemctl edit postgresql-16"
# Look at systemd.unit(5) manual page for more info.# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-16-setup.
[Unit]
Description=PostgreSQL 16 database server
Documentation=https://www.postgresql.org/docs/16/static/
After=syslog.target
After=network-online.target[Service]
Type=notifyUser=postgres
Group=postgres# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/16/data/
Environment=PGDATA=/pgdata/data/pg16/# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog# Disable OOM kill on postgres main process
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0#ExecStartPre=/usr/pgsql-16/bin/postgresql-16-check-db-dir ${PGDATA}
ExecStart=/pgsql/app/pg16/bin/postgres -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT# Do not set any timeout value, so that systemd will not kill postgres 
# main process during crash recovery.
TimeoutSec=0# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0TimeoutStopSec=1h[Install]
WantedBy=multi-user.target
  • 重新加載配置文件 systemctl start postgresql-16.service 不會結束命令,systemctl status postgresql-16.service Active: activating (start)顯示狀態非running
# reload配置文件
[root@pgdb ~]# systemctl daemon-reload
# 啟動數據庫
[root@pgdb ~]# systemctl start postgresql-16.service
# 開機自啟動
[root@pgdb ~]# systemctl enable postgresql-16.service
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-16.service → /usr/lib/systemd/system/postgresql-16.service.
# pg14 資源狀態
[root@pgdb ~]# systemctl status postgresql-16.service
● postgresql-16.service - PostgreSQL 16 database serverLoaded: loaded (/usr/lib/systemd/system/postgresql-16.service; enabled; vendor preset: disabled)Active: activating (start) since Wed 2025-03-19 16:21:17 CST; 3min 33s agoDocs: https://www.postgresql.org/docs/16/static/Main PID: 1062 (postgres)Tasks: 6 (limit: 37638)Memory: 11.9MCGroup: /system.slice/postgresql-16.service├─1062 /pgsql/app/pg16/bin/postgres -D /pgdata/data/pg16/├─1067 postgres: checkpointer ├─1068 postgres: background writer ├─1070 postgres: walwriter ├─1071 postgres: autovacuum launcher └─1072 postgres: logical replication launcher Mar 19 16:21:17 pgdb systemd[1]: Starting PostgreSQL 16 database server...
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.178 CST [1062] LOG:  starting PostgreSQL 16.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.4.1 20200928 (Re>
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.179 CST [1062] LOG:  listening on IPv6 address "::1", port 5436
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.179 CST [1062] LOG:  listening on IPv4 address "127.0.0.1", port 5436
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.180 CST [1062] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5436"
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.184 CST [1069] LOG:  database system was shut down at 2025-03-19 16:19:41 CST
Mar 19 16:21:17 pgdb postgres[1062]: 2025-03-19 16:21:17.187 CST [1062] LOG:  database system is ready to accept connections

2.6 編輯環境變量

#PG 16
######
export PGPORT=5436
export PGDATA=/pgdata/data/pg16
export PGHOME=/pgsql/app/pg16

3. 安裝后配置

3.1 修改默認用戶postgres密碼

postgres=# alter user postgres with password 'postgres';
ALTER ROLE

3.2 連接配置文件

  • postgresql.conf參數 監聽所有地址IP
[postgres@pgdb pg16]$ cd $PGDATA
[postgres@pgdb pg16]$ vi postgresql.conf
#listen_addresses = 'localhost'         # what IP address(es) to listen on;  #重啟生效
listen_addresses = '*'
  • pg_hba.conf參數 實例訪問控制
[postgres@pgdb pg16]$ cd $PGDATA
[postgres@pgdb pg16]$ vi pg_hba.conf
# IPv4 local connections:
#host    all             all             127.0.0.1/32            trust   #重加載配置生效 
# 允許192.168.1.0網段連接數據庫
host    all             all             192.168.1.0/24          scram-sha-256
  • 重啟數據庫
[postgres@pgdb ~]$ pg_ctl restart
  • 遠程連接數據庫
[postgres@pgdb ~]$ psql -h 192.168.1.109 -p 5436 -U postgres -d postgres

3.3 數據庫啟動關閉

[postgres@pgdb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1063)
/pgsql/app/pg16/bin/postgres "-D" "/pgdata/data/pg16/"
[postgres@pgdb ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres@pgdb ~]$ pg_ctl start
waiting for server to start....2025-03-21 22:24:33.069 CST [1335] LOG:  redirecting log output to logging collector process
2025-03-21 22:24:33.069 CST [1335] HINT:  Future log output will appear in directory "log".done
server started
[postgres@pgdb ~]$ pg_ctl status
pg_ctl: server is running (PID: 1335)
/pgsql/app/pg16/bin/postgres

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/899085.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/899085.shtml
英文地址,請注明出處:http://en.pswp.cn/news/899085.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

(基本常識)C++中const與引用——面試常問

作者&#xff1a;求一個demo 版權聲明&#xff1a;著作權歸作者所有&#xff0c;商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處 內容通俗易懂&#xff0c;沒有廢話&#xff0c;文章最后是面試常問內容&#xff08;建議通過標題目錄學習&#xff09; 廢話不多…

Java并發編程 什么是分布式鎖 跟其他的鎖有什么區別 底層原理 實戰講解

目錄 一、分布式鎖的定義與核心作用 二、分布式鎖與普通鎖的核心區別 三、分布式鎖的底層原理與實現方式 1. 核心實現原理 2. 主流實現方案對比 3. 關鍵技術細節 四、典型問題與解決方案 五、總結 六、具體代碼實現 一、分布式鎖的定義與核心作用 分布式鎖是一種在分布…

案例:使用網絡命名空間模擬多主機并通過網橋訪問外部網絡

案例目標 隔離性&#xff1a;在同一臺物理機上創建兩個獨立的網絡命名空間&#xff08;模擬兩臺主機&#xff09;&#xff0c;確保其網絡配置完全隔離。內部通信&#xff1a;允許兩個命名空間通過虛擬設備直接通信。外部訪問&#xff1a;通過宿主機的網橋和 NAT 規則&#xff…

AF3 Rotation 類解讀

Rotation 類(rigid_utils 模塊)是 AlphaFold3 中用于 3D旋轉 的核心組件,支持兩種旋轉表示: 1?? 旋轉矩陣 (3x3) 2?? 四元數 (quaternion, 4元向量) ?? 設計目標: 允許靈活選擇 旋轉矩陣 或 四元數 封裝了常用的 旋轉操作(組合、逆旋轉、應用到點上等) 像 torch.…

DeepSeek面試——模型架構和主要創新點

本文將介紹DeepSeek的模型架構多頭潛在注意力&#xff08;MLA&#xff09;技術&#xff0c;混合專家&#xff08;MoE&#xff09;架構&#xff0c; 無輔助損失負載均衡技術&#xff0c;多Token 預測&#xff08;MTP&#xff09;策略。 一、模型架構 DeepSeek-R1的基本架構沿用…

【web3】

檢測錢包是否安裝 方法一 // npm install metamask/detect-provider import detectEthereumProvider from metamask/detect-provider// 檢測錢包是否安裝 const isProvider await detectEthereumProvider() if(!isProvider) {proxy.$modal.msgError("請安裝錢包")…

husky的簡介以及如果想要放飛自我的解決方案

husky 是一個 Git Hooks 管理工具&#xff0c;它的主要作用是 在 Git 提交&#xff08;commit&#xff09;、推送&#xff08;push&#xff09;等操作時執行自定義腳本&#xff0c;比如代碼檢查&#xff08;Lint&#xff09;、單元測試&#xff08;Test&#xff09;、格式化代碼…

JVM之類的加載過程

加載 這一階段是將類的字節碼從外部存儲&#xff08;如磁盤&#xff09;加載到JVM的內存中。加載時&#xff0c;JVM會根據類的全限定名&#xff08;包括包名和類名&#xff09;查找相應的字節碼文件&#xff08;.class文件&#xff09;&#xff0c;并將其讀入內存。 鏈接 鏈接…

Java Collection API增強功能系列之六 改進的 ConcurrentHashMap:歸約、搜索、計數與 Set 視圖詳解

Java 8 改進的 ConcurrentHashMap&#xff1a;歸約、搜索、計數與 Set 視圖詳解 Java 8 對 ConcurrentHashMap 進行了重大優化&#xff0c;不僅提升了并發性能&#xff0c;還引入了許多函數式編程方法&#xff0c;使其在處理高并發場景時更加高效和靈活。本文將深入解析 Concu…

AI生成移動端貪吃蛇游戲頁面,手機瀏覽器打開即可玩

貪吃蛇游戲可計分&#xff0c;可穿墻&#xff0c;AI生成適配手機瀏覽器的游戲&#xff0c;代碼如下&#xff1a; <!DOCTYPE html> <html lang"zh-CN"> <head> <meta charset"UTF-8"> <meta name"viewport" …

【動手學深度學習】#4 深度學習計算

主要參考學習資料&#xff1a; 《動手學深度學習》阿斯頓張 等 著 【動手學深度學習 PyTorch版】嗶哩嗶哩跟李牧學AI 概述 為了實現更復雜的網絡&#xff0c;我們需要研究比層更高一級的單元塊&#xff0c;在編程中由類表示。通過自定義層和塊&#xff0c;我們能更靈活地搭建網…

如何在 Windows 上安裝并使用 Postman?

Postman 是一個功能強大的API測試工具&#xff0c;它可以幫助程序員更輕松地測試和調試 API。在本文中&#xff0c;我們將討論如何在 Windows 上安裝和使用 Postman。 Windows 如何安裝和使用 Postman 教程&#xff1f;

php寫入\查詢influxdb數據

namespace app\index\controller;use InfluxDB2\Client; use InfluxDB2\Model\WritePrecision; use InfluxDB2\Point;class Demo {/*** 顯示資源列表** return \think\Response*/public function index(){$token 你的TOKEN;$org zzlichi;$bucket initdb;$client new Client…

26考研——圖_圖的基本概念(6)

408答疑 文章目錄 一、圖的基本概念圖的定義非空性非線性結構 頂點和邊的表示頂點邊 有向圖 & 無向圖有向圖有向圖 G 1 G_1 G1? 的表示 無向圖無向圖 G 2 G_2 G2? 的表示 簡單圖 & 多重圖簡單圖多重圖 頂點的度、入度和出度頂點的度有向圖的度 路徑、路徑長度和回路…

面向對象軟件工程實踐軟件案例

智力運動-數字化思維訓練課程介紹 數字化思維訓練是科技賦能素質教育創新實踐項目&#xff0c;通過數字化信息化手段&#xff0c;深度融合優質原創智力運動教育課程資源&#xff0c;服務幼兒園與小學&#xff0c;提供信息時代校園素質教育教學解決方案。在《面向對象軟件工程》…

Linux學習筆記(應用篇一)

基于I.MX6ULL.MINI開發板 標準I/O庫鏈接目錄刪除文件正則表達式系統標識時間堆內存信號標準信號 進程進程組進程間通信線程互斥鎖線程安全 本文章是入門篇的概念&#xff0c;有點零散&#xff0c;后續需要補充復習 **inode&#xff08;索引節點&#xff09;**是 Linux 和 Unix …

Modbus RTU ---> Modbus TCP透傳技術實現(Modbus透傳、RS485透傳、RTU透傳)分站代碼實現、協議轉換器

文章目錄 Modbus RTU到Modbus TCP透傳技術實現1. 透傳技術概述1.1 透傳基本原理- 協議幀格式轉換- 地址映射與管理- 通信時序適配- 錯誤檢測與處理 2. 透傳網關硬件架構2.1 典型硬件結構- 微控制器/處理器(ARM、STM32等)- RS-485/RS-232收發器- 以太網控制器(如W5500)- 電源管理…

MySQL數據庫中常用的命令

登錄&#xff1a; mysql -u username -h ip地址 -P 端口 -p 密碼 mysql -u username -S /path/mysql.sock -P -p 用戶管理&#xff1a; select user,host from mysql.user;//查看數據庫中所用用戶信息 create user username%;//創建用戶 create user username% identifie…

醫學交互作用分析步驟和目的(R語言)

醫學交互作用分析的目的和用途&#xff08;R語言&#xff09; 醫學交互作用分析一直是醫學數據分析的組成部分&#xff0c;總結最近的一些認識。 目的&#xff1a; 在獨立危險因素鑒定的研究中&#xff0c;&#xff08;獨立危險因素的&#xff09;交互作用可以作為獨立危險因…

Javaweb后端登錄會話技術jwt令牌

jwt生成與校驗 是base4補位的 最后面是簽名&#xff0c;簽名不是base64&#xff0c;是通過簽名算法加密后來的 令牌長度不是固定的&#xff0c;長度取決于原始內容&#xff0c;載荷&#xff0c;大小 頭有&#xff0c;類型&#xff0c;簽名算法 base64可以對任意的二進制數據進…