Search code examples
cpostgresqlcompilationcc

Error while loading shared libraries: libpq.so.5


I am trying to write a basic client for Postgres integration in C using libpq, I compiled the libraries following the Client-only installation on this page and so far so good, I compile my program with the following:

cc -o connect connect.c -I/usr/local/pgsql/include -L/usr/local/pgsql/lib -lpq

and by running the program this is the output:

./connect: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory

I correctly compiled all the libraries and libpq.so.5 is on the system: plocate libpq.so.5 return /usr/local/pgsql/lib/libpq.so.5

Could anyone help me with this? What am I missing? Thanks in advance


Solution

  • You have the library installed in a non-standard path. You need to tell the linker where to find it by setting LD_LIBRARY_PATH:

    $ export LD_LIBRARY_PATH=/usr/local/pgsql/lib
    $ ./client
    

    If you are using bash then you can use this construct to append a value:

    $ export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}${LD_LIBRARY_PATH:+:}/usr/local/pgsql/lib"