0. 前言
本文主要記錄原始碼安裝postgresql的程序,打開了debug相關內容,方便后續通過gdb進行除錯,同時包含了設定postgresql系統服務相關內容,以及配置多個資料庫實體的內容,
1. 環境資訊
centos 7 2004, postgresql 12.4
2. 編譯安裝
2.1解壓原始碼
su
unzip postgres-master.zip
mv postgres-master /usr/local/pgsql
2.2 安裝依賴包
yum -y install gcc gcc-c++ automake autoconf libtool make readline-devel zlib-devel readline felx bison
2.3 編譯安裝
cd /usr/local/pgsql/
./configure --prefix=/usr/local/pgsql --enable-debug --enable-cassert --enable-depend CFLAGS=-O0
make
make install
- -prefix : 指定安裝的目錄
- --enable-debug : 把所有程式和庫以帶有除錯符號的方式編譯,相當于gcc的-g
- --enable-cassert : 打開服務器中的斷言(assertion)檢查,它會檢查許多”不可能發生”的條件,
- -enable-depend : 打開自動依賴性跟蹤,
- CFLAGS=-O0 : 關閉優化
3. 初始化資料庫
3.1 添加資料庫用戶
adduser postgres
passwd postgres
chown -R postgres:root /usr/local/pgsql
3.2 初始化資料庫
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
* postgresql支持同時運行多個資料庫實體,每個實體對應自己的資料庫目錄,監聽各自的埠,可以通過多次執行此步驟,初始化多個資料庫實體,只需要指定不同的資料庫目錄,如/usr/local/pgsql/data1
4. 配置資料庫
4.1 資料庫訪問設定
設定為允許所有連接
> vim /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 trust
4.2 偵聽所有連接
> vim /usr/local/pgsql/data/postgresql.conf
listen_addresses = '*'
logging_collector = on
4.3 修改監聽埠
> vim /usr/local/pgsql/data/postgresql.conf
port = 5432
* 如果初始化多個資料庫實體,每個實體需要指定不同的埠號,如5433
4.4 設定環境變數
> vim ~/.bashrc
export PATH=$PATH:/usr/local/pgsql/bin
export PGDATA=/usr/local/pgsql/data
> source ~/.bashrc
5. 啟動資料庫
啟動資料庫
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
連接資料庫
[postgres@centos8-pg12 ~]$ psql
psql (14devel)
Type "help" for help.
postgres=#
* 如果初始化多個資料庫實體,每個實體需要指定不同的埠號,如5433
// connect to pg listening on 5432
psql -p 5432 -U postgres
// connect to pg listening on 5433
psql -p 5433 -U postgres
創建新的資料庫
createdb -p 5432 test
// connect to pg listening on 5432, database test
psql -p 5432 -U test
* 如果不通過-p指定埠號,則會默認連接5432, 也可以通過下面配置指定psql使用的埠號,資料庫用戶等
> vi ~/.bashrc
# the default pg host
PGHOST=localhost
# the default pg port
PGPORT=5432
# the default pg user
PGUSER=postgres
6. 服務設定
上述啟動資料庫為手動啟動,系統重啟后不會自動啟動,下面方法可以添加postgres服務,并設定開機自啟動,
6.1 添加啟動服務
切換到root下
su
cp /usr/local/pgsql/contrib/start-scripts/linux /etc/init.d/postgresql
chmod u+x /etc/init.d/postgresql
* 如果初始化多個資料庫實體,需要為每個實體準備單獨的服務腳本,如/etc/init.d/postgresql1
確認PGDATA與初始化資料庫時指定的目錄一致
> vi /etc/init.d/postgresql
PGDATA="/usr/local/pgsql/data"
6.2 添加開機自啟動
chkconfig --add postgresql
可以通過service控制postgres服務
service postgresql start
service postgresql stop
service postgresql restart
7. 引申配置
一些optional的配置
> vi ~/.bashrc
# used for gdb
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
# alias for multiple db instances
alias psql1='psql -p 5432 -U postgres'
alias psql2='psql -p 5433 -U postgres'
alias psql3='psql -p 5434 -U postgres'
8. Trouble-shooting
1. make時報找不到bison
config前沒有安裝bison,make時報找不到bison,這時再安裝bison再make依舊報錯,原因是config時會設定BISON環境變數,make時會使用它,因此如果config時沒有安裝bison,config后再安裝bison,次變數依然為空,make時依舊會報找不到bison,解決方法很簡單,安裝bison后重新config,
make -C parser gram.h
make[2]: Entering directory '/usr/local/pgsql/src/backend/parser'
'/usr/bin/perl' ./check_keywords.pl gram.y ../../../src/include/parser/kwlist.h
***
ERROR: `bison' is missing on your system. It is needed to create the
file `gram.c'. You can either get bison from a GNU mirror site
or download an official distribution of PostgreSQL, which contains
pre-packaged bison output.
***
2. psql中無法通過上下箭頭呼叫歷史命令,也無法編輯歷史命令
原因是config時添加了選項--without-readline,詳見官方說明
--without-readline
Prevents use of the Readline library (and libedit as well). This option disables command-line editing and history in psql.
./configure --prefix=/usr/local/pgsql --without-readline
8. 參考資料
https://developer.aliyun.com/article/696187
https://www.postgresql.org/docs/current/install-procedure.html
https://www.linuxcool.com/createdb
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/240115.html
標籤:其他
上一篇:PostgreSql從庫重新配置
下一篇:mysql精華
