Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

89.2. postgresql-18.6

PostgreSQL relational database server

Version 18.6, release 1
Shelf database — Database engines, embedded key-value stores, database servers, clients and full-text indexes
Phase 41_system (System), 334 of 1047 in build order
Licence PostgreSQL
Homepage https://www.postgresql.org/
Build time 6.8 SBU
Installed size 37.4 MiB in 1693 files
Recipe ports/core/database/postgresql/kpkgbuild

About

PostgreSQL is the relational database server, with its client psql, its administration tools (initdb, pg_dump, pg_basebackup and the rest), the contrib modules, and libpq, the client library that gdal, qgis, libreoffice, python3-psycopg2 and the QPSQL driver of qt6-qtbase connect through. On KDOS it runs as one shared server under the postgres account, started by the service 76_postgresql once a cluster exists; see Services.

Why it is in KDOS

The comment above it in script/phases/41_system/packages.d/database.txt:

musl's collation is essentially byte order, so ORDER BY, LIKE and unique indexes on case-folded text all quietly disagree with every other machine the data touches. --with-icu is what makes them correct.

Sources

Dependencies

Depends on: pkgconf, perl, python3, readline, zlib, openssl, icu, lz4, zstd, libxml2, libxslt, docbook-xsl, docbook-xml, tzdata, util-linux, curl, pam, krb5, liburing, openldap.

Required by (7): gdal, gnuhealth, libreoffice, pdal, python3-psycopg2, qgis, qt6-qtbase.

Optional, off in KDOS

These switches in build.sh turn off a feature whose library is a port here. The recipe may turn each off on purpose, or because the library was not yet a port when the recipe was written; this list cannot tell which.

Installation

From the repository root, inside the build container, this is the line kdosbuild runs for the port:

script/chroot/exec.sh bash -c 'source script/phases/41_system/phase.env && kpkg install postgresql'

What kpkg does

kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/database/postgresql.

1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.

cd /ports/core/database/postgresql
echo '555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f  postgresql-18.6.tar.bz2' | sha256sum -c -

2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.

SRC_ROOT=/var/cache/kpkg/work/postgresql
SRC=$SRC_ROOT/postgresql-18.6
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf postgresql-18.6.tar.bz2 -C "$SRC" --strip-components=1

Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.

3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:

./configure \
	--prefix=/usr \
	--sysconfdir=/etc \
	--libdir=/usr/lib \
	--datarootdir=/usr/share \
	--with-icu \
	--with-openssl \
	--with-readline \
	--with-libxml \
	--with-libxslt \
	--with-lz4 \
	--with-zstd \
	--with-python \
	--with-perl \
	--with-pam \
	--with-gssapi \
	--with-libcurl \
	--with-liburing \
	--with-uuid=e2fs \
	--with-system-tzdata=/usr/share/zoneinfo \
	--without-tcl \
	--with-ldap \
	--without-llvm \
	--disable-rpath
make world-bin
make -C doc/src/sgml man
make DESTDIR=$PKG install-world-bin
make -C doc/src/sgml DESTDIR=$PKG install-man

install -d -m 700 "$PKG/var/lib/postgres"
install -d "$PKG/etc/init.d"
cat > "$PKG/etc/init.d/76_postgresql.sh" <<'KDOS_SH'
#!/bin/bash
. /etc/init.d/service_helper

NAME="postgresql"
DAEMON="/usr/bin/postgres"
DATA="/var/lib/postgres/data"

case "$1" in
    start)
        [ ! -x "$DAEMON" ] && { echo "[SKIP] $NAME: $DAEMON not found"; exit 0; }
        if ! getent passwd postgres >/dev/null; then
            echo "[SKIP] $NAME: no postgres account (the port's postinstall makes it)"
            exit 0
        fi
        if [ ! -s "$DATA/PG_VERSION" ]; then
            echo "[SKIP] $NAME: no cluster in $DATA (initdb makes one)"
            exit 0
        fi
        echo "[KDOS] Starting $NAME..."
        supervise --stop-grace 30 "$NAME" setpriv --reuid=postgres \
            --regid=postgres --init-groups "$DAEMON" -D "$DATA"
        ;;
    stop)   stop_service "$NAME" ;;
    status) check_status "$NAME" ;;
    *)      echo "Usage: $0 {start|stop|status}"; exit 1 ;;
esac
KDOS_SH
chmod 755 "$PKG/etc/init.d/76_postgresql.sh"

The switches, and why each is set:

The common switches of each build system are explained in the Flag glossary.

postinstall.sh runs on the installed system after the package is adopted:

# The server runs as this account and refuses root, and initdb writes the
# cluster as whoever runs it, so the account owns the data directory: a cluster
# owned by anyone else is one postgres refuses to open.
#
# Everything is written under PKG_ROOT, the root kpkgadd is installing into:
# --root and an A/B update install into a tree that is not the running one, and
# an account made in / instead would be missing from the system that needs it.
#
# The guard reads PKG_ROOT's files: getent answers for the running root, not
# the tree being installed, and would let useradd run against an account the
# target already has.
root="${PKG_ROOT:-/}"
grep -q '^postgres:' "$root/etc/group" 2>/dev/null || groupadd -R "$root" -r postgres
grep -q '^postgres:' "$root/etc/passwd" 2>/dev/null || \
	useradd -R "$root" -r -g postgres -d /var/lib/postgres -s /sbin/nologin postgres

# chown resolves a name against the running root's passwd, so the ids are read
# out of PKG_ROOT's.
# The mode is set as well: kpkgadd creates every directory 0755, whatever mode
# it was packaged with, because the package carries no group to give it.
while IFS=: read -r n _ uid gid _; do
	[ "$n" = postgres ] || continue
	chown "$uid:$gid" "$root/var/lib/postgres"
	chmod 700 "$root/var/lib/postgres"
done < "$root/etc/passwd"

4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/postgresql, prunes what the image does not ship, lints the tree and rolls it reproducibly:

tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
    --mtime=@$SOURCE_DATE_EPOCH \
    --use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
    -cf postgresql-18.6-1.tar.xz -C "$PKG" .

5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/postgresql, and runs the triggers its paths call for.

Contents

Installed programs: clusterdb, createdb, createuser, dropdb, dropuser, ecpg, initdb, oid2name, pg_amcheck, pg_archivecleanup, pg_basebackup, pg_checksums, pg_combinebackup, pg_config, pg_controldata, pg_createsubscriber, pg_ctl, pg_dump, pg_dumpall, pg_isready, pg_receivewal, pg_recvlogical, pg_resetwal, pg_restore, pg_rewind, pg_test_fsync, pg_test_timing, pg_upgrade, pg_verifybackup, pg_waldump, pg_walsummary, pgbench, postgres, psql, reindexdb, vacuumdb, vacuumlo.

Installed libraries: libecpg.a, libecpg.so, libecpg_compat.a, libecpg_compat.so, libpgcommon.a, libpgcommon_shlib.a, libpgfeutils.a, libpgport.a, libpgport_shlib.a, libpgtypes.a, libpgtypes.so, libpq-oauth-18.so, libpq-oauth.a, libpq.a, libpq.so.

Installed directories: /usr/include/libpq, /usr/include/postgresql, /usr/lib/postgresql, /usr/share/doc/postgresql, /usr/share/licenses/postgresql, /usr/share/postgresql, /var/lib/postgres.

Short descriptions

clusterdb cluster a PostgreSQL database
createdb create a new PostgreSQL database
createuser define a new PostgreSQL user account
dropdb remove a PostgreSQL database
dropuser remove a PostgreSQL user account
ecpg embedded SQL C preprocessor
initdb create a new PostgreSQL database cluster
oid2name resolve OIDs and file nodes in a PostgreSQL data directory
pg_amcheck checks for corruption in one or more PostgreSQL databases
pg_archivecleanup clean up PostgreSQL WAL archive files
pg_basebackup take a base backup of a PostgreSQL cluster
pg_checksums enable, disable or check data checksums in a PostgreSQL database cluster
pg_combinebackup reconstruct a full backup from an incremental backup and dependent backups
pg_config retrieve information about the installed version of PostgreSQL
pg_controldata display control information of a PostgreSQL database cluster
pg_createsubscriber convert a physical replica into a new logical replica
pg_ctl initialize, start, stop, or control a PostgreSQL server
pg_dump export a PostgreSQL database as an SQL script or to other formats
pg_dumpall extract a PostgreSQL database cluster into a script file
pg_isready check the connection status of a PostgreSQL server
pg_receivewal stream write-ahead logs from a PostgreSQL server
pg_recvlogical control PostgreSQL logical decoding streams
pg_resetwal reset the write-ahead log and other control information of a PostgreSQL database cluster
pg_restore restore a PostgreSQL database from an archive file created by pg_dump
pg_rewind synchronize a PostgreSQL data directory with another data directory that was forked from it
pg_test_fsync determine fastest wal_sync_method for PostgreSQL
pg_test_timing measure timing overhead
pg_upgrade upgrade a PostgreSQL server instance
pg_verifybackup verify the integrity of a base backup of a PostgreSQL cluster
pg_waldump display a human-readable rendering of the write-ahead log of a PostgreSQL database cluster
pg_walsummary print contents of WAL summary files
pgbench run a benchmark test on PostgreSQL
postgres PostgreSQL database server
psql PostgreSQL interactive terminal
reindexdb reindex a PostgreSQL database
vacuumdb garbage-collect and analyze a PostgreSQL database
vacuumlo remove orphaned large objects from a PostgreSQL database
libecpg.pc PostgreSQL libecpg library
libecpg_compat.pc PostgreSQL libecpg_compat library
libpgtypes.pc PostgreSQL libpgtypes library
libpq.pc PostgreSQL libpq library

Notes

make world-bin builds the server, the clients and every contrib module but no documentation, and make install-world-bin installs the same. The manual pages are built and installed on their own from doc/src/sgml with libxslt’s xsltproc and the DocBook stylesheets and DTD in depends.

build.sh writes the service script /etc/init.d/76_postgresql.sh. It skips itself until the postgres account exists and /var/lib/postgres/data holds a cluster (sudo -u postgres initdb -D /var/lib/postgres/data makes one), then supervises postgres -D /var/lib/postgres/data run as postgres through setpriv. It looks the account up with getent passwd postgres. It passes supervise --stop-grace 30: SIGTERM is PostgreSQL’s smart shutdown, which waits for the open sessions to end and then writes a checkpoint, and the supervisor’s default five seconds would SIGKILL the server partway through and leave the next start to crash recovery.

postinstall.sh creates the postgres group and account with /var/lib/postgres as its home, and gives that directory to it at mode 0700. The package carries the directory at that mode already, but kpkgadd creates every directory 0755, so the hook sets it again; see postinstall.sh.