Thread: Building postgresql from sources, statically linked, linux

Building postgresql from sources, statically linked, linux

From
Rob Gansevles
Date:
Hi,

I would like to have a statically linked linux x64 version of postgresql 14.1 for creating a distributable sample database.

I could not find it anywhere for downloading (someone has a link maybe?), so I tried to build one from the sources.

I am following the instructions on https://www.postgresql.org/docs/14/install-short.html and to get a statically linked binary, I add the LDFLAGS='--static' argument to the configure statement.
I guess that is the way do this.

In stead of building on my laptop, I use docker so that building does not depend on what I have installed.
With this Dockerfile building fails.

==== Dockerfile ====
FROM ubuntu:20.04
RUN  apt-get update && apt-get install -y build-essential libreadline-dev zlib1g-dev
COPY postgresql-14.1.tar.gz .
RUN tar -xvzf postgresql-14.1.tar.gz
RUN rm postgresql-14.1.tar.gz
WORKDIR postgresql-14.1
RUN ./configure LDFLAGS='--static'
RUN make
RUN make install
RUN adduser postgres
RUN mkdir /usr/local/pgsql/data
RUN chown postgres /usr/local/pgsql/data
USER postgres
RUN /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

==== end of Dockerfile ====


==== output from docker build ====
.......
make -C backend/utils/mb/conversion_procs all
make[2]: Entering directory '/postgresql-14.1/src/backend/utils/mb/conversion_procs'
make -C cyrillic_and_mic all
make[3]: Entering directory '/postgresql-14.1/src/backend/utils/mb/conversion_procs/cyrillic_and_mic'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -I../../../../../../src/include  -D_GNU_SOURCE   -c -o cyrillic_and_mic.o cyrillic_and_mic.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -O2 -fPIC -shared -o cyrillic_and_mic.so cyrillic_and_mic.o  -L../../../../../../src/port -L../../../../../../src/common  --static  -Wl,--as-needed  
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
collect2: error: ld returned 1 exit status
make[3]: *** [../../../../../../src/Makefile.shlib:293: cyrillic_and_mic.so] Error 1
make[3]: Leaving directory '/postgresql-14.1/src/backend/utils/mb/conversion_procs/cyrillic_and_mic'
make[2]: *** [Makefile:25: all-cyrillic_and_mic-recurse] Error 2
make[2]: Leaving directory '/postgresql-14.1/src/backend/utils/mb/conversion_procs'
make[1]: *** [Makefile:42: all-backend/utils/mb/conversion_procs-recurse] Error 2
make[1]: Leaving directory '/postgresql-14.1/src'
make: *** [GNUmakefile:11: all-src-recurse] Error 2
The command '/bin/sh -c make' returned a non-zero code: 2
==== end of output from docker build ====


Why does this error happen?

I seem to be able to work around it by copying some libs: copy /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o to /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginT.o
A new attempt with this Dockerfile does build the software, but when it then fails with initdb:


==== 2nd Dockerfile ====
FROM ubuntu:20.04
RUN  apt-get update && apt-get install -y build-essential libreadline-dev zlib1g-dev
COPY postgresql-14.1.tar.gz .
RUN tar -xvzf postgresql-14.1.tar.gz
RUN rm postgresql-14.1.tar.gz
WORKDIR postgresql-14.1
RUN cp /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o
RUN ./configure LDFLAGS='--static'
RUN make
RUN make install
RUN adduser postgres
RUN mkdir /usr/local/pgsql/data
RUN chown postgres /usr/local/pgsql/data
USER postgres
RUN /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
==== end of 2nd Dockerfile ====

Initdb output:

======== 2nd docker build output =======
......
Step 15/15 : RUN /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
 ---> Running in 8c4fc574766e
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".
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 disabled.

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... sysv
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... 2021-12-11 19:08:58.765 UTC [16] FATAL:  could not load library "/usr/local/pgsql/lib/dict_snowball.so": /usr/local/pgsql/lib/dict_snowball.so: undefined symbol: CurrentMemoryContext
2021-12-11 19:08:58.765 UTC [16] STATEMENT:  CREATE FUNCTION dsnowball_init(INTERNAL)
            RETURNS INTERNAL AS '$libdir/dict_snowball', 'dsnowball_init'
        LANGUAGE C STRICT;

child process exited with exit code 1
initdb: removing contents of data directory "/usr/local/pgsql/data"
The command '/bin/sh -c /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data' returned a non-zero code: 1
======== end of 2nd docker build output =======


Anybody has an idea why this does not work or what I am doing wrong and how I can get a statically linked binary?

Without the LDFLAGS the build and the initdb work fine.


Thanks in advance,

Rob






Re: Building postgresql from sources, statically linked, linux

From
Tom Lane
Date:
Rob Gansevles <rgansevles@gmail.com> writes:
> I would like to have a statically linked linux x64 version of postgresql
> 14.1 for creating a distributable sample database.

If you are trying to make the server into a monolithic object, that's
not something we support or have any particular interest in supporting.
Extensions such as PLs are always built as shared libraries.  So are
the encoding conversion modules, which your build is failing on before
it gets to any others.  It would take a good deal of fooling around
to change that, both in the build process and in the way that the
core server invokes that functionality.

Statically linking client-side programs such as psql is more within
the realm of feasibility, but you'd have to adjust just those builds
rather than trying to apply --static across the board.

            regards, tom lane