MultipassでPostgreSQL 18をソースコードからビルドして起動する
TechBull Advent Calendar 2025 9日目の記事です。
はじめに
今回は、MultipassにPostgreSQLのコードをcloneして、実際にビルドして動かしてみました。
一部変更していますが、基本的にはPostgreSQLのドキュメントにも書かれている手順でおこなっているためこちらもぜひ参照ください!
環境
- PC
- CPU : Apple M2 Max
- OS : macOS Tahoe 26.1
Multipassのセットアップ
Multipassをインストールする
各環境でインストールしてください。
参考 (https://canonical.com/multipass/install)
Ubuntu インスタンスを作成する
PostgreSQL 18をビルドするために、Ubuntu仮想マシンを作成します。
Ubuntu24.04 LTSを使って、インスタンスを作成します。
multipass launch 24.04 \
--name pg18-build \
--cpus 4 \
--memory 4G \
--disk 20G
VMにログインする
multipass shell または SSHを使ってログインできます。
- multipass shellを使う方法
multipass shell pg18-build
- SSHを使う方法
// IPを確認する
multipass info pg18-build
// 表示されたIPを指定してログインする
ssh ubuntu@192.168.xx.xx
PostgreSQL 18のソースコードを取得する
Multipass上のUbuntuインスタンスにログインできたら、PostgreSQL 18のソースコードを取得します。
PostgreSQLをcloneする
まず、作業用のディレクトリを作成し、リポジトリからコードをcloneします。
mkdir -p ~/work/pg18
cd ~/work/pg18
git clone https://github.com/postgres/postgres.git
cd postgres
cloneが完了したら、PostgreSQL 18のブランチに変更します。
git checkout REL_18_STABLE
ビルドに必要なパッケージをインストールする
モジュールをインストールする
# ローカルのパッケージリストを更新する
sudo apt update
# 前提のモジュールのインストールする
sudo apt install -y \
build-essential \
flex bison \
pkg-config \
libicu-dev \
libreadline-dev \
zlib1g-dev
PostgreSQLのビルドとインストール
まずはconfigureを実行し、ビルド設定をします。
configureのオプションはこちらに一覧があります。
./configure
configureが成功したら、makeを実行します。
ここでは、PostgreSQL本体、クライアントやライブラリのコンパイルを行います。
make
make installするために、rootユーザーにします。
ビルド成果物をインストール先へコピーします。
sudo su -
cd /home/ubuntu/work/pg18/postgres
make install
ビルド成果物のコピーが完了したら、PostgreSQLの実行ユーザーを作成します。
adduser postgres
PostgreSQLのデータディレクトリを作成する。
合わせて、ディレクトリの所有者をpostgresユーザーに変更します。
mkdir -p /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
ディレクトリが作成完了したら、postgresユーザーに切り替えます。
sudo su postgres
クラスタの初期化と起動を確認する
ビルドとインストールが終わったら、クラスタの初期化をして実際に起動させていきます。
まず、データベースクラスタを初期化します。
postgres@pg18-build:~$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
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 "C.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 enabled.
fixing permissions on existing directory /usr/local/pgsql/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/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:
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
PostgreSQLを起動する
初期化が完了したらあとは、起動するだけです!
postgres@pg18-build:~$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
waiting for server to start.... done
server started
server started となったので、PostgreSQLに接続してみましょう。
postgres@pg18-build:~$ /usr/local/pgsql/bin/psql -d postgres
psql (18.1)
Type "help" for help.
postgres=#
これで、PostgreSQL18.1に接続することができました!
まとめ
Autoconf and Make を使って実際にPostgreSQLをビルドして動かしてみました。
(今回は使用しませんでしたがMeson でもビルドが可能です。)
今後は、実際にPostgreSQLのコードを読みながら内部実装を追っていきたいと思います。
ビルドする際の参考になれば嬉しいです!
参考
Discussion