Skip to content

Commit c60447c

Browse files
committed
Build PostgreSQL from source for pg_lake internal headers
pg_lake requires PostgreSQL internal headers (like server/rewrite/rewriteManip.h) that are not available in pre-built packages. This follows pg_lake's official Dockerfile approach of building PostgreSQL from source. Changes: - Use debian:bookworm-slim as base instead of postgres:17-bookworm - Build PostgreSQL 17.2 from source with required configure flags - Include simple entrypoint script for database initialization - Add all required runtime dependencies
1 parent 8538c9e commit c60447c

1 file changed

Lines changed: 122 additions & 32 deletions

File tree

Dockerfile.pg_lake

Lines changed: 122 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,118 @@
11
# PostgreSQL image with pgvector, PostGIS, pg_textsearch, pgsodium, and pg_lake
2-
# Uses Debian base for pg_lake compatibility (requires full PostgreSQL server headers)
2+
# Uses Debian base with PostgreSQL built from source (required for pg_lake internal headers)
33
# Multi-stage build - all toolchains discarded, only artifacts kept
44

5-
ARG PG_VERSION=17
5+
ARG PG_VERSION=17.2
66
ARG PGVECTOR_VERSION=0.8.0
77
ARG POSTGIS_VERSION=3.5.1
88
ARG PG_TEXTSEARCH_VERSION=0.2.0
99
ARG PGSODIUM_VERSION=3.1.9
1010
ARG PG_LAKE_VERSION=main
1111

1212
#############################################
13-
# Stage 1: Build extensions
13+
# Stage 1: Build PostgreSQL and extensions
1414
#############################################
15-
FROM postgres:${PG_VERSION}-bookworm AS builder
15+
FROM debian:bookworm-slim AS builder
1616

17+
ARG PG_VERSION
1718
ARG PGVECTOR_VERSION
1819
ARG POSTGIS_VERSION
1920
ARG PG_TEXTSEARCH_VERSION
2021
ARG PGSODIUM_VERSION
2122
ARG PG_LAKE_VERSION
2223

24+
# Install build dependencies
2325
RUN apt-get update && apt-get install -y --no-install-recommends \
2426
git \
2527
build-essential \
26-
postgresql-server-dev-17 \
27-
llvm-16-dev \
28-
clang-16 \
2928
curl \
3029
ca-certificates \
30+
wget \
31+
# PostgreSQL build dependencies
32+
libreadline-dev \
33+
zlib1g-dev \
34+
libssl-dev \
35+
libxml2-dev \
36+
libxslt1-dev \
37+
libicu-dev \
38+
uuid-dev \
39+
liblz4-dev \
40+
flex \
41+
bison \
42+
pkg-config \
3143
# PostGIS dependencies
3244
libgeos-dev \
3345
libproj-dev \
3446
libgdal-dev \
3547
libjson-c-dev \
3648
libprotobuf-c-dev \
3749
protobuf-c-compiler \
38-
libxml2-dev \
3950
libpcre2-dev \
40-
libsodium-dev \
41-
# PostGIS build tools
4251
perl \
43-
flex \
44-
bison \
52+
# pgsodium dependencies
53+
libsodium-dev \
4554
# pg_lake dependencies
4655
cmake \
4756
ninja-build \
48-
pkg-config \
49-
libssl-dev \
5057
libsnappy-dev \
5158
libjansson-dev \
52-
liblz4-dev \
5359
liblzma-dev \
5460
libzstd-dev \
55-
libpq-dev \
5661
libkrb5-dev \
62+
libcurl4-openssl-dev \
5763
&& rm -rf /var/lib/apt/lists/*
5864

5965
WORKDIR /build
6066

67+
# Build PostgreSQL from source (required for pg_lake internal headers)
68+
ENV PGBASEDIR=/usr/local/pgsql
69+
RUN wget https://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.gz && \
70+
tar -xzf postgresql-${PG_VERSION}.tar.gz && \
71+
cd postgresql-${PG_VERSION} && \
72+
./configure --prefix=${PGBASEDIR} \
73+
--with-openssl \
74+
--with-libxml \
75+
--with-libxslt \
76+
--with-icu \
77+
--with-uuid=ossp \
78+
--with-lz4 && \
79+
make -j$(nproc) && \
80+
make install && \
81+
cd contrib && make -j$(nproc) install && \
82+
cd ../.. && rm -rf postgresql-${PG_VERSION}*
83+
84+
# Set PATH for pg_config
85+
ENV PATH="${PGBASEDIR}/bin:${PATH}"
86+
6187
# pgvector
6288
RUN git clone --branch v${PGVECTOR_VERSION} --depth 1 https://github.com/pgvector/pgvector.git && \
6389
cd pgvector && \
6490
make OPTFLAGS="" -j$(nproc) && \
65-
make install
91+
make install && \
92+
cd .. && rm -rf pgvector
6693

6794
# PostGIS (without raster and topology for smaller build)
6895
RUN curl -L https://download.osgeo.org/postgis/source/postgis-${POSTGIS_VERSION}.tar.gz | tar xz && \
6996
cd postgis-${POSTGIS_VERSION} && \
70-
./configure --without-raster --without-topology && \
97+
./configure --without-raster --without-topology --with-pgconfig=${PGBASEDIR}/bin/pg_config && \
7198
make && \
72-
make install
99+
make install && \
100+
cd .. && rm -rf postgis-${POSTGIS_VERSION}
73101

74102
# pg_textsearch (BM25)
75103
RUN git clone --branch v${PG_TEXTSEARCH_VERSION} --depth 1 https://github.com/timescale/pg_textsearch.git && \
76104
cd pg_textsearch && \
77-
# Fix missing math.h include (upstream bug)
78105
sed -i '1i #include <math.h>' src/am/build.c && \
79106
make -j$(nproc) && \
80-
make install
107+
make install && \
108+
cd .. && rm -rf pg_textsearch
81109

82110
# pgsodium
83111
RUN git clone --branch v${PGSODIUM_VERSION} --depth 1 https://github.com/michelp/pgsodium.git && \
84112
cd pgsodium && \
85113
make -j$(nproc) && \
86-
make install
114+
make install && \
115+
cd .. && rm -rf pgsodium
87116

88117
# pg_lake - Postgres with Iceberg and data lake access
89118
RUN git clone --branch ${PG_LAKE_VERSION} --depth 1 --recurse-submodules https://github.com/Snowflake-Labs/pg_lake.git && \
@@ -102,40 +131,101 @@ RUN git clone --branch ${PG_LAKE_VERSION} --depth 1 --recurse-submodules https:/
102131
make -C pg_lake_copy && make -C pg_lake_copy install && \
103132
make -C pg_lake_iceberg && make -C pg_lake_iceberg install && \
104133
make -C pg_lake_table && make -C pg_lake_table install && \
105-
make -C pg_lake && make -C pg_lake install
134+
make -C pg_lake && make -C pg_lake install && \
135+
cd .. && rm -rf pg_lake
106136

107137
#############################################
108138
# Stage 2: Final runtime image
109139
#############################################
110-
FROM postgres:${PG_VERSION}-bookworm
140+
FROM debian:bookworm-slim
111141

112142
# Runtime deps only
113143
RUN apt-get update && apt-get install -y --no-install-recommends \
144+
libreadline8 \
145+
zlib1g \
146+
libssl3 \
147+
libxml2 \
148+
libxslt1.1 \
149+
libicu72 \
150+
libossp-uuid16 \
151+
liblz4-1 \
152+
# PostGIS runtime
114153
libgeos-c1v5 \
115154
libproj25 \
116155
libgdal32 \
117156
libjson-c5 \
118157
libprotobuf-c1 \
119-
libxml2 \
120158
libpcre2-8-0 \
159+
# pgsodium runtime
121160
libsodium23 \
122161
# pg_lake runtime dependencies
123162
libsnappy1v5 \
124163
libjansson4 \
125-
liblz4-1 \
126164
liblzma5 \
127165
libzstd1 \
128-
libpq5 \
129-
&& rm -rf /var/lib/apt/lists/*
166+
libkrb5-3 \
167+
libcurl4 \
168+
gosu \
169+
locales \
170+
&& rm -rf /var/lib/apt/lists/* \
171+
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
172+
173+
ENV LANG en_US.utf8
174+
175+
# Create postgres user
176+
RUN groupadd -r postgres --gid=999 && \
177+
useradd -r -g postgres --uid=999 --home-dir=/var/lib/postgresql --shell=/bin/bash postgres && \
178+
mkdir -p /var/lib/postgresql && \
179+
chown -R postgres:postgres /var/lib/postgresql
180+
181+
# Copy PostgreSQL installation from builder
182+
COPY --from=builder /usr/local/pgsql /usr/local/pgsql
130183

131-
# Copy compiled extensions from builder
132-
COPY --from=builder /usr/lib/postgresql/17/lib/ /usr/lib/postgresql/17/lib/
133-
COPY --from=builder /usr/share/postgresql/17/extension/ /usr/share/postgresql/17/extension/
134184
# Copy avro library for pg_lake
135185
COPY --from=builder /usr/local/lib/libavro* /usr/local/lib/
136186

187+
# Set up paths and environment
188+
ENV PATH="/usr/local/pgsql/bin:${PATH}"
189+
ENV PGDATA=/var/lib/postgresql/data
190+
ENV LD_LIBRARY_PATH="/usr/local/lib"
191+
137192
# Update library cache
138193
RUN ldconfig
139194

195+
# Create data directory
196+
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
197+
198+
# Create run directory for socket
199+
RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 3777 /var/run/postgresql
200+
201+
VOLUME /var/lib/postgresql/data
202+
203+
EXPOSE 5432
204+
205+
# Simple entrypoint that initializes DB if needed and starts postgres
206+
COPY --chmod=755 <<'EOF' /usr/local/bin/docker-entrypoint.sh
207+
#!/bin/bash
208+
set -e
209+
210+
# If PGDATA is empty, initialize the database
211+
if [ -z "$(ls -A "$PGDATA" 2>/dev/null)" ]; then
212+
gosu postgres initdb --username=postgres --pwfile=<(echo "${POSTGRES_PASSWORD:-postgres}")
213+
214+
# Allow connections from anywhere
215+
echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
216+
echo "listen_addresses = '*'" >> "$PGDATA/postgresql.conf"
217+
fi
218+
219+
# If first arg is postgres or not provided, run postgres
220+
if [ "$1" = 'postgres' ] || [ -z "$1" ]; then
221+
exec gosu postgres postgres -D "$PGDATA"
222+
fi
223+
224+
exec "$@"
225+
EOF
226+
227+
ENTRYPOINT ["docker-entrypoint.sh"]
228+
CMD ["postgres"]
229+
140230
LABEL org.opencontainers.image.source="https://github.com/constructive-io/docker"
141-
LABEL org.opencontainers.image.description="PostgreSQL 17 with pgvector, PostGIS, pg_textsearch, pgsodium, and pg_lake (Debian-based)"
231+
LABEL org.opencontainers.image.description="PostgreSQL 17 with pgvector, PostGIS, pg_textsearch, pgsodium, and pg_lake (Debian-based, built from source)"

0 commit comments

Comments
 (0)