Lazarus

Programming => Databases => Topic started by: lainz on October 05, 2021, 02:00:05 am

Title: PostgreSQL on Linux
Post by: lainz on October 05, 2021, 02:00:05 am
Hi, I've downloaded the .so files from this site
https://www.enterprisedb.com/download-postgresql-binaries

And I want to use them with my executable on Linux. I place the .so files in the same folder as the executable and it always ask me the same .so file 'libpq.so.5' in an error message.

I have the same program running on Windows without problems.
Title: Re: PostgreSQL on Linux
Post by: PierceNg on October 05, 2021, 03:05:44 am
By default, Linux doesn't look for shared library files in the directory where the executable is located. There is a standard list of locations for that. This standard list is not identical across different Linux distros.

Two possible solutions:

- Instead of downloading the PostgreSQL binaries, use your Linux distro's package manager to install the PostgreSQL client package. This will place the .so files in the right places on the system with the right permissions.

- Execute your Pascal program from a shell script that sets LD_LIBRARY_PATH to the directory where the Pascal executable and PostgreSQL .so files are located. Working example for Ubuntu:

Code: Text  [Select][+][-]
  1. #!/bin/sh
  2. MYPASPROGPATH=$(/bin/dirname `/bin/readlink -f "$0"`)
  3. LD_LIBRARY_PATH="$MYPASPROGPATH" exec "$MYPASPROGPATH/mypasprog" $@ &

The first line sets MYPASPROGPATH to the directory that contains your executable and the .so files. The second line sets LD_LIBRARY_PATH to that same directory, then runs your program (named "mypasprog" in the script). In this way, "mypasprog" will look for the .so files in that directory.
Title: Re: PostgreSQL on Linux
Post by: lainz on October 07, 2021, 05:00:21 pm
Ok thanks, then better use the installed library.

There's a way to detect at runtime if the library is installed?
Title: Re: PostgreSQL on Linux
Post by: MarkMLl on October 07, 2021, 07:12:43 pm
I would strongly suggest using the libraries packaged by whatever distro you're using.

At that point you might find that the ordinary client package installs various .so files, but that you can benefit from (also) installing the developer package since it sets up a symlink from a more generic name to the specific files supplied:

Code: [Select]
$ locate libpq | grep /usr/lib/
/usr/lib/x86_64-linux-gnu/libpq.a
/usr/lib/x86_64-linux-gnu/libpq.so
/usr/lib/x86_64-linux-gnu/libpq.so.5
/usr/lib/x86_64-linux-gnu/libpq.so.5.11
/usr/lib/x86_64-linux-gnu/pkgconfig/libpq.pc

$ cd /usr/lib/x86_64-linux-gnu

$ find . -iname 'libpq*' -ls
 44092818    452 -rw-r--r--   1 root     root       462106 May 12 15:42 ./libpq.a
 44046882    296 -rw-r--r--   1 root     root       301040 May 12 15:42 ./libpq.so.5.11
 45620852      4 -rw-r--r--   1 root     root          319 May 12 15:42 ./pkgconfig/libpq.pc
 44092820      0 lrwxrwxrwx   1 root     root           13 May 12 15:42 ./libpq.so -> libpq.so.5.11
 44092832      0 lrwxrwxrwx   1 root     root           13 May 12 15:42 ./libpq.so.5 -> libpq.so.5.11

I believe that some of the pain has been reduced by the addition of a library loader component, but I've not investigated recently.

MarkMLl
Title: Re: PostgreSQL on Linux
Post by: devEric69 on October 07, 2021, 09:41:30 pm
I don't know PostgreSQL. Nevertheless, here are some informations:
- there is a short summary about the techniques on how to dynamically load a shared library (https://wiki.freepascal.org/Lazarus/FPC_Libraries#Simplified_overview_of_the_system.2C_when_loading_a_shared_library_under_Linux) from an *.elf application, on the *nixes, and on Linux in particular.
- there's a freeware named Debreate (https://antumdeluge.github.io/debreate-web/) in order to create a binary installation package (*.deb), under Linux (in which one can code a bash script for example - #! /bin/bash -e - in its proposed post-install event, among other events; we can code inside an "un-gzip", an "un-tar" a tree-archive, create a soft-symlink, launch an "apt-get install *.deb" of an already existing and shipped\included binary package dropped inside the\your said current installation, ...).
- from one embedded to another, maybe: you could look at the installation discussions concerning the Firebird embedded applications. It's possible that the same recipes work with the embedded PostgreSQL version, too (that said, I don't know if you're using such an embedded PostgreSQL's version).
Title: Re: PostgreSQL on Linux
Post by: MarkMLl on October 07, 2021, 11:11:29 pm
I don't know PostgreSQL.

I do. Been using it on Linux for 20+ years. And the major gotcha (as far as clients are concerned) is that symlink.

MarkMLl
TinyPortal © 2005-2018