Home > Enterprise >  Creating a standalone, relocatable build of postgres
Creating a standalone, relocatable build of postgres

Time:02-02

For a small project I'm working on, I would like to create a “relocatable build” of PostgreSQL, similar to the binaries objdumps

My understanding is that I should be getting the source code for these libraries (and their dependencies) and compiling them statically.

As things stand currently, my build script is quite barebones and obviously produces an install that is linked against whatever distribution it was run on:

./configure \
  --prefix="${outputDir}" \
  --with-uuid="ossp"

I'm wondering if anyone could outline what steps I must take to get the relocatable build that I'm after. My hunch right now is that I'm looking for guidance on what environment variables I would need to set and/or parameters I'd need to provide to my build in order to end up with a fully relocatable build of Postgres.

Please note: I don't normally work with C/C although I have several years of ./configure, make and doing builds for other much higher level ecosystems under my belt. I'm well aware that distribution-specific releases of Postgres are widely available, to speak nothing of the official docker container. Please take the approach that I'm pursuing a concept in the spirit of research or exploration. I'm looking for a precise solution, not a fast one.

CodePudding user response:

This answer is for Linux; this will work differently on different operating systems.

You can create a “relocatable build” of PostgreSQL if you build it with the appropriate “run path”. The documentation gives you these hints:

The method to set the shared library search path varies between platforms, but the most widely-used method is to set the environment variable LD_LIBRARY_PATH [...]

On some systems it might be preferable to set the environment variable LD_RUN_PATH before building.

The manual for ld tells you:

If -rpath is not used when linking an ELF executable, the contents > of the environment variable LD_RUN_PATH will be used if it is defined.

It also tells you this about the run path:

The tokens $ORIGIN and $LIB can appear in these search directories. They will be replaced by the full path to the directory containing the program or shared object in the case of $ORIGIN and either lib - for 32-bit binaries - or lib64 - for 64-bit binaries - in the case of $LIB.

See also this useful answer.

So the sequence of steps would be:

./configure --disable-rpath [other options]
export LD_RUN_PATH='$ORIGIN/../lib'
make
make install

Then you package the PostgreSQL binaries in the bin subdirectory and the shared libraries plus all required libraries (you can find them with ldd) in the lib subdirectory. The libraries will then be looked up relative to the binaries.

  •  Tags:  
  • Related