以下の内容はhttps://rabbitfoot141.hatenablog.com/entry/2026/01/18/232507より取得しました。


PostgreSQL のビルドメモ

はじめに

どうも、最近は MySQL と格闘するより k8s と格闘してるけんつです。そろそろ MySQL とも仲を深めたいところですね。
さぁそんなこんなで今日はなんとびっくりポスグレ話です。なんでこんなことになってるかって、最近は仕事で MySQL Operator を色々やってると他の DBMS Operator がどうなっているかということが気になり、有名なのといえば cloudnative-pg では?ということでまず PostgreSQL をやってみるかという話になったわけです。

準備

何はともあれまずはソースコードを持ってくる。まずはこれであれある。

$ git clone git@github.com:postgres/postgres.git

次はビルドに必要な手順のドキュメントを探す。これである。
www.postgresql.org

ビルド祭り

事前確認

ドキュメントを順に見ていくとまず make 3.81 以降が要求されるとのことで確認する。

$ make --version
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

次は色々あるが bison 2.3 以上を要求される。

$ apt list | grep bison

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

bison++/noble 1.21.11-5 amd64
bison-doc/noble,noble 1:3.8.2+repack-1 all
bison/noble,now 2:3.8.2+dfsg-1build2 amd64 [installed]

これ以上は ./configure がいい感じにしてくれるらしい。ちなみにその時のログは config.log に出る。

ビルドである

必要なパッケージ群がよくわからなかったのでとりあえず configure を実行してみる。cmake でないのが新鮮だ。

$ mkdir build && cd $_
build$ ../configure


icu 系が足りないと言われているので入れる。

checking for icu-uc icu-i18n... no
configure: error: Package requirements (icu-uc icu-i18n) were not met:

Package 'icu-uc', required by 'virtual:world', not found
Package 'icu-i18n', required by 'virtual:world', not found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ICU_CFLAGS
and ICU_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
$ sudo apt install libicu74 libicu-dev

次は flex がないと怒られたので入れる。

$ sudo apt install flex

次は readline がないと言われたので入れる。

$ sudo apt install libreadline8t64 libreadline-dev

通ったのでオッケーである。

$ ../configure
...
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking ../src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking ../src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking ../src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking ../src/makefiles/Makefile.linux to src/Makefile.port

しかしここで、 debug ビルドしていないことに気がついたので debug ビルドを設定して make する

build$ ../configure --enable-debug --enable-cassert
build$ make -j$(proc)

するとこういったディレクトリ構成になっていることがわかる。

build$ ll          
total 352
drwxrwxr-x  6 lrf141 lrf141   4096 Jan 18 22:32 ./
drwxrwxr-x  9 lrf141 lrf141   4096 Jan 18 22:32 ../
-rw-rw-r--  1 lrf141 lrf141   4176 Jan 18 22:32 GNUmakefile
lrwxrwxrwx  1 lrf141 lrf141     48 Jan 18 22:32 Makefile -> /home/lrf141/postgresqlProject/postgres/Makefile
drwxrwxr-x  2 lrf141 lrf141   4096 Jan 18 22:32 config/
-rw-rw-r--  1 lrf141 lrf141 284291 Jan 18 22:32 config.log
-rwxrwxr-x  1 lrf141 lrf141  39264 Jan 18 22:32 config.status*
drwxrwxr-x 61 lrf141 lrf141   4096 Jan 18 22:32 contrib/
drwxrwxr-x  3 lrf141 lrf141   4096 Jan 18 22:32 doc/
drwxrwxr-x 16 lrf141 lrf141   4096 Jan 18 22:32 src/

MySQLer としてはここから make install するような手順を走らせたくないので色々調べると、src/bin ディレクトリ以下に生成されたバイナリが固まっていて PostgreSQL 本体(?) は src/backend 下にあるらしい。
というわけで初期化を試す。

$ mkdir pgdata
$ ./src/bin/initdb/initdb -D pgdata
./src/bin/initdb/initdb: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory

すると見事にライブラリを見つけられていないのでどうにかする必要がある。
まず見つからないと言われているライブラリはここにある。

$ ll src/interfaces/libpq
total 4052
drwxrwxr-x 5 lrf141 lrf141    4096 Jan 18 22:41 ./
drwxrwxr-x 5 lrf141 lrf141    4096 Jan 18 22:39 ../
lrwxrwxrwx 1 lrf141 lrf141      69 Jan 18 22:39 Makefile -> /home/lrf141/postgresqlProject/postgres/src/interfaces/libpq/Makefile
-rw-rw-r-- 1 lrf141 lrf141    3346 Jan 18 22:41 exports.list
-rw-rw-r-- 1 lrf141 lrf141   85952 Jan 18 22:41 fe-auth-oauth.o
-rw-rw-r-- 1 lrf141 lrf141   85952 Jan 18 22:41 fe-auth-oauth_shlib.o
-rw-rw-r-- 1 lrf141 lrf141   72256 Jan 18 22:41 fe-auth-scram.o
-rw-rw-r-- 1 lrf141 lrf141   76288 Jan 18 22:41 fe-auth.o
-rw-rw-r-- 1 lrf141 lrf141   62864 Jan 18 22:41 fe-cancel.o
-rw-rw-r-- 1 lrf141 lrf141  292168 Jan 18 22:41 fe-connect.o
-rw-rw-r-- 1 lrf141 lrf141  219376 Jan 18 22:41 fe-exec.o
-rw-rw-r-- 1 lrf141 lrf141   74968 Jan 18 22:41 fe-lobj.o
-rw-rw-r-- 1 lrf141 lrf141   76744 Jan 18 22:41 fe-misc.o
-rw-rw-r-- 1 lrf141 lrf141   65424 Jan 18 22:41 fe-print.o
-rw-rw-r-- 1 lrf141 lrf141  140136 Jan 18 22:41 fe-protocol3.o
-rw-rw-r-- 1 lrf141 lrf141   48856 Jan 18 22:41 fe-secure.o
-rw-rw-r-- 1 lrf141 lrf141  135288 Jan 18 22:41 fe-trace.o
-rw-rw-r-- 1 lrf141 lrf141    8920 Jan 18 22:41 legacy-pqsignal.o
-rw-rw-r-- 1 lrf141 lrf141   35080 Jan 18 22:41 libpq-events.o
-rw-rw-r-- 1 lrf141 lrf141       0 Jan 18 22:41 libpq-refs-stamp
-rw-rw-r-- 1 lrf141 lrf141 1419720 Jan 18 22:41 libpq.a
-rw-rw-r-- 1 lrf141 lrf141     318 Jan 18 22:41 libpq.pc
lrwxrwxrwx 1 lrf141 lrf141      13 Jan 18 22:41 libpq.so -> libpq.so.5.19*
lrwxrwxrwx 1 lrf141 lrf141      13 Jan 18 22:41 libpq.so.5 -> libpq.so.5.19*
-rwxrwxr-x 1 lrf141 lrf141 1166304 Jan 18 22:41 libpq.so.5.19*
drwxrwxr-x 2 lrf141 lrf141    4096 Jan 18 22:39 po/
-rw-rw-r-- 1 lrf141 lrf141   18912 Jan 18 22:41 pqexpbuffer.o
drwxrwxr-x 2 lrf141 lrf141    4096 Jan 18 22:39 t/
drwxrwxr-x 2 lrf141 lrf141    4096 Jan 18 22:39 test/

ここでしばらく詰まったのだが、所見ですべてを理解するのは面倒だったので --prefix で build ディレクトリを指定し make install する手段に出る。

build$ ../configure --enable-debug --enable-cassert --prefix=$(pwd)
build$ make -j$(nproc)
build$ make install

ここまで出たら起動しようと思ったが初期化が必要だそう。

$ mkdir pgdata     
build$ ./bin/postgres -D pgdata
postgres: could not access the server configuration file "/home/lrf141/postgresqlProject/postgres/build/pgdata/postgresql.conf": No such file or directory
build$ ./bin/initdb -D pgdata
The files belonging to this database system will be owned by user "lrf141".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are enabled.

fixing permissions on existing directory pgdata ... 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/Tokyo
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
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:

    bin/pg_ctl -D pgdata -l logfile start

どうやらいけたようだ

build$ ./bin/pg_ctl -D pgdata -l logfile start
waiting for server to start.... done
server started
build$ ps aux | grep postgres
lrf141   1199798  0.0  0.0 207120 24664 ?        Ss   23:14   0:00 /home/lrf141/postgresqlProject/postgres/build/bin/postgres -D pgdata
lrf141   1199799  0.0  0.0 207252  5696 ?        Ss   23:14   0:00 postgres: io worker 0
lrf141   1199800  0.0  0.0 207252  4124 ?        Ss   23:14   0:00 postgres: io worker 1
lrf141   1199801  0.0  0.0 207120  3660 ?        Ss   23:14   0:00 postgres: io worker 2
lrf141   1199802  0.0  0.0 207252  3700 ?        Ss   23:14   0:00 postgres: checkpointer 
lrf141   1199803  0.0  0.0 207280  4700 ?        Ss   23:14   0:00 postgres: background writer 
lrf141   1199805  0.0  0.0 207252  8092 ?        Ss   23:14   0:00 postgres: walwriter 
lrf141   1199806  0.0  0.0 208708  6964 ?        Ss   23:14   0:00 postgres: autovacuum launcher 
lrf141   1199807  0.0  0.0 208572  6188 ?        Ss   23:14   0:00 postgres: logical replication launcher 
lrf141   1210033  0.0  0.0   3528  1888 pts/0    S+   23:15   0:00 grep --color=auto postgres

というわけでログインしてみる。

build$ ./bin/psql postgres             
psql (19devel)
Type "help" for help.


postgres=# select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 19devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, 64-bit
(1 row)

勝利。

最終的な手順まとめ

MySQL を日常的にビルド・デバッグしているため完全ではないが少なくとも自分の環境では

$ sudo apt install libicu74 libicu-dev flex libreadline8t64 libreadline-dev
$ mkdir build && cd $_
$ ../configure --enable-debug --enable-cassert --prefix=$(nproc)
$ make -j$(nproc)
$ make install
$ mkdir pgdata
$ ./bin/initdb -D pgdata
$ ./bin/pg_ctl -D pgdata -l logfile start
$ ./bin/psql postgres

でいけた。テストを走らせたい場合は make check を make 以降で実施すると良いらしい。

おわりに

MySQL 風に make install なしで PostgreSQL を build ディレクトリ内で完結させて動かしたかったがいい方法を思いつかなかった。
まぁこれはおいおいということで。むしろ誰かいい方法があったら教えてほしい。地味に make install が長い。

ところでこの記事を書いて思ったが、いつになったら cloudnative-pg にたどり着けるのだろうか…。

追記 2026/01/20

元気に色々やろうとしたら愛用している IDE の CLion が autotools ベースの PostgreSQL ビルドにおいてコード解析を走らせられないということがわかったので meson 版をまとめておく。
ドキュメントは最初に参照したやつで問題ない。

$ mkdir build-meson && cd $_
$ meson setup .. --buildtype=debug -Dcassert=true --prefix=$(pwd)
$ ninja
$ ninja install



以上の内容はhttps://rabbitfoot141.hatenablog.com/entry/2026/01/18/232507より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14