1. 下載源代碼
- 首先在https://github.com/上找到postgre項目源碼,我?guī)湍阏业搅耍?/li>
-
clone 源代碼:點擊下圖位置,copy倉庫URL。
- 在Linux下clone代碼。
git clone https://github.com/postgres/postgres.git
2. 配置
如果不熟悉源代碼目錄結構,可以看文章:《第 3 課 PostgreSQL代碼結構》,我這里就不贅述了。
進入根目錄下,執(zhí)行下面命令:
$./configure --prefix=`pwd`/release --with-openssl --without-ldap --with-libxml --enable-thread-safety --enable-debug
--prefix=`pwd`/release: 指定安裝目錄為當前目錄下的release。
--enable-debug : 保留語法符號,避免install時目標文件被strip掉了符號,調試時無法看到堆棧函數(shù)和變量名。
configure 過程中會出現(xiàn)很多依賴包找不到,如果你連了網,你可以自己安裝需要包。
$yum install xxxx;
如果不知道全名和選擇那個平臺可以查找:
$yum search xxxx;
如果你先前已經執(zhí)行./configure過,但是你想重新來過,你可以使用下面的命令恢復原狀:
$ make distclean
$make clean只是清除編譯產生的中間和目標文件,make distclean可以清除configure產生的文件和安裝目錄,反正就是恢復到初始狀態(tài)。
./configrue完成后,在目錄下會產生一些新的文件文件。
- configure前:
$ ls
aclocal.m4 config configure configure.in contrib COPYRIGHT doc GNUmakefile.in HISTORY Makefile README README.git src
- configure后:
$ ls
aclocal.m4 config config.log config.status configure configure.in contrib COPYRIGHT doc GNUmakefile GNUmakefile.in HISTORY Makefile README README.git src
我們關注需要的文件:GNUmakefile, src/Makefile.global。GNUmakefile是由GNUmakefile.in模板生成。Makefile.global是由Makefile.global.in模板文件生成而來。
修改Makefile.global中的編譯選項:O2改為O0,這樣編譯出來未優(yōu)化,方便調試代碼。
- 修改前:
260 CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-
aliasing -fwrapv -fexcess-precision=standard -g -O2
261 CFLAGS_VECTOR = -funroll-loops -ftree-vectorize
262 CFLAGS_SSE42 = -msse4.2
263 CFLAGS_ARMV8_CRC32C =
264 CXXFLAGS = -Wall -Wpointer-arith -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O2
- 修改后:
260 CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-
aliasing -fwrapv -fexcess-precision=standard -g -O0
261 CFLAGS_VECTOR = -funroll-loops -ftree-vectorize
262 CFLAGS_SSE42 = -msse4.2
263 CFLAGS_ARMV8_CRC32C =
264 CXXFLAGS = -Wall -Wpointer-arith -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -g -O0
3. 編譯代碼
在根目錄下,執(zhí)行:
$ make -j4
如果你是多核虛擬機,你可以-jx, x就是你的核數(shù),可以加快編譯速度,第一次編譯大約需要3~5分鐘。
4. 安裝
$make install
會安裝到你configure配置的--prefix指定的目錄,我是在當前目錄的release下:
$ ls
bin include lib share
進入bin目錄,初始化服務:
$ ./initdb -D ../data -Uwangwei -W
Enter new superuser password:
啟動:
$ ./pg_ctl start -D ../data
查看進程:
$ ps -ef|grep postgres
image.png
5. 連接服務器
>$ ./psql -d postgres -Upostgres -W
Password:
postgres=# select version();
version
------------------------------------------------------------------------------------------------------------
PostgreSQL 12devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28), 64-bit
(1 row)
postgres=# \dn+
List of schemas
Name | Owner | Access privileges | Description
--------+----------+----------------------+------------------------
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
(1 row)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
發(fā)現(xiàn)更多寶藏
我在喜馬拉雅上分享聲音
《PostgreSQL數(shù)據庫內核分析》,點開鏈接可以聽聽,有點意思。
《數(shù)據庫系統(tǒng)概論(第4版)》,點開鏈接可以聽聽,有點意思。
其他相關文章分享列表:
第 23 課 PostgreSQL 創(chuàng)建自己的數(shù)據庫、模式、用戶
第 22 課 PostgreSQL 控制文件
第 21 課 PostgreSQL 日志系統(tǒng)
第 16 課 查詢過程源碼分析
第 15 課 PostgreSQL 系統(tǒng)參數(shù)配置
第 14 課 PostgreSQL 數(shù)據存儲結構
第 13 課 PostgreSQL 存儲之Page(頁面)源碼分析
第 12 課 PostgreSQL 認證方式
第 11 課 PostgreSQL 增加一個內核C函數(shù)
第 10 課 PostgreSQL 在內核增加一個配置參數(shù)
第 09 課 PostgreSQL 4種進程啟動方式
第 08 課 PostgreSQL 事務介紹
第 07 課 PostgreSQL 數(shù)據庫、模式、表、空間、用戶間的關系
第 06 課 PostgreSQL 系統(tǒng)表介紹
第 05 課 PostgreSQL 編譯源代碼進行開發(fā)
第 04 課 PostgreSQL 安裝最新的版本
第 03 課 PostgreSQL 代碼結構
第 02 課 PostgreSQL 的特性、應用、安裝
第 01 課 PostgreSQL 簡介及發(fā)展歷程
上面文章都在專輯中:PostgreSQL專輯鏈接,點我查看
如果有用,可以收藏這篇文件,隨時在更新....
更多交流加群: PostgreSQL內核開發(fā)群 876673220
親,記得點贊、留言、打賞額?。?!

