Lazarus

Programming => Databases => Topic started by: young_nandy on December 08, 2018, 04:45:23 pm

Title: Can not load the PostgreSQL client library "libpq.dll" [SOLVED]
Post by: young_nandy on December 08, 2018, 04:45:23 pm
Hello,

I can't connect to postgresql database. error as below:

Can not load the PostgreSQL client library "libpq.dll". Check your installation

I have tried installing C: \ Program Files (x86) \ PostgreSQL \ 10 \ bin in the system environment and copying the libpq.dll file in the lazarus folder and application folder, but it still can't connect.

I use Windows 10 64bit and Postgres 10.6 32bit

can anyone help me?
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: zeljko on December 08, 2018, 05:10:15 pm
If your application is 64bit then it cannot work with 32bit .dll (or vice versa).
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on December 08, 2018, 11:47:32 pm
If your application is 64bit then it cannot work with 32bit .dll (or vice versa).

i use 64bit lazarus and postgresql 64 bit also the same can't connect?
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: marcov on December 08, 2018, 11:53:37 pm
That should work. Also it is only the clientlib part.  The clientlib can be 32-bit and the server 64-bit with no problem.

If you doublechecked that both the lazarus generated application and the DLL are the same architecture (both 32-bit or both 64-bit), it might be that the libpq.dll itself depends on other dlls, like libssl  (libeay32 etc)
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on December 09, 2018, 12:22:58 am
What should I do ? I have no idea to solve this problem. I've searched Google but the answer is floating. between the path settings in environment variables and copy the libpq.dll file on the lazarus folder or copy it to my application folder
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: 440bx on December 09, 2018, 12:37:52 am
Obviously you need to track down the reason why the DLL cannot be loaded.  Following on Marco's idea, I suggest you do the following:

1. add a call to LoadLibrary at the beginning of your program where you "manually" try to load the library.  If LoadLibrary fails then, it is most likely that, as Marco mentioned, one or more of the library dependencies are not found.

2. if the call fails then use a utility such as objdump/pedump/dumpbin to determine the library dependencies and add calls to LoadLibrary for each one of them.  Any one of those that fail will cause your target dll load to fail.

3. You may want to copy those into your program's/executable's folder (to ensure they can be found.) Proceeding this way, you'll find out which dll is causing the problem and lead to a solution.

HTH.
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on December 09, 2018, 02:02:53 am
Obviously you need to track down the reason why the DLL cannot be loaded.  Following on Marco's idea, I suggest you do the following:

1. add a call to LoadLibrary at the beginning of your program where you "manually" try to load the library.  If LoadLibrary fails then, it is most likely that, as Marco mentioned, one or more of the library dependencies are not found.

2. if the call fails then use a utility such as objdump/pedump/dumpbin to determine the library dependencies and add calls to LoadLibrary for each one of them.  Any one of those that fail will cause your target dll load to fail.

3. You may want to copy those into your program's/executable's folder (to ensure they can be found.) Proceeding this way, you'll find out which dll is causing the problem and lead to a solution.

HTH.

how do I check the libpq.dll in windows 10 dependency?
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: 440bx on December 09, 2018, 03:08:40 am
how do I check the libpq.dll in windows 10 dependency?
Do you know how to use a PE file viewer ?  ... if you don't, it will be a bit difficult for you to carry out my suggestions but... there is a graphical, easy to use PE file viewer, CFF Explorer written by Daniel Pistelli, you can download a free copy at https://ntcore.com/?page_id=388 (https://ntcore.com/?page_id=388)

Things you want to look at are:

1. make sure the bitness of the DLL you want to use matches the bitness of your program. 
2. Look at the DLL's import section.  You'll see a list of other DLLs it depends on.  Every one of those DLLs must be loadable in order for the dependent dll to load successfully.  Make sure you find every dll on that list in your system.  if any are missing, the dll won't load (also, make sure the bitness of those you find matches the bitness of the dll you want to use/load.)

CFF Explorer will enable you to carry steps 1 and 2 easily.

3. To figure out if Windows can find the DLL(s) for you, put a LoadLibrary call in your code for the dll.  If the name of the DLL is correct in that call and, the call fails, the most common reasons are a.) Windows cannot find the dll and/or b.) the bitness of the dll it found does not match the bitness of your program.

HTH.

ETA: if you specify the full path in the LoadLibrary call then Windows should find it.  If the load fails, it may be because of a bitness mismatch, a dependency on another dll that is not satisfied or, uncommon but possible, the dll is corrupt.



Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on December 09, 2018, 06:36:57 am
Obviously you need to track down the reason why the DLL cannot be loaded.  Following on Marco's idea, I suggest you do the following:

1. add a call to LoadLibrary at the beginning of your program where you "manually" try to load the library.  If LoadLibrary fails then, it is most likely that, as Marco mentioned, one or more of the library dependencies are not found.

2. if the call fails then use a utility such as objdump/pedump/dumpbin to determine the library dependencies and add calls to LoadLibrary for each one of them.  Any one of those that fail will cause your target dll load to fail.

3. You may want to copy those into your program's/executable's folder (to ensure they can be found.) Proceeding this way, you'll find out which dll is causing the problem and lead to a solution.

HTH.

I have got the dependency file from libpq.dll.

but I don't understand how to "add a call to LoadLibrary at the beginning of your program where you" manually "try to load the library"?

can you help me with an example?

the third method has been tried but there is still an error
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: dinmil on December 09, 2018, 07:19:19 am
Only coping libpq.dll is not enough. This library depend on mscvrt???? redistribute library. I suggest that you start installation of postgres 10 or any other and you will see that first thing it do is make installation of ms redistributed library. I thing that the best thing during installation is to remove everything (stack buider, server ...) and install only utilities. This will install postgres client correctly
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on December 09, 2018, 07:25:00 am
thank you all, I've been able to solve this problem.

I moved Lazarus & Postgres to the 32bit version. And the problem is solved by itself. This also solved my problem at embarcadero 10.2.3 32 bit which is exactly the same.

I still don't understand the solution to this problem, but I accept using the 32-bit version.
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: taazz on December 09, 2018, 08:06:30 am
Obviously you need to track down the reason why the DLL cannot be loaded.  Following on Marco's idea, I suggest you do the following:

1. add a call to LoadLibrary at the beginning of your program where you "manually" try to load the library.  If LoadLibrary fails then, it is most likely that, as Marco mentioned, one or more of the library dependencies are not found.

2. if the call fails then use a utility such as objdump/pedump/dumpbin to determine the library dependencies and add calls to LoadLibrary for each one of them.  Any one of those that fail will cause your target dll load to fail.

3. You may want to copy those into your program's/executable's folder (to ensure they can be found.) Proceeding this way, you'll find out which dll is causing the problem and lead to a solution.

HTH.

I have got the dependency file from libpq.dll.

but I don't understand how to "add a call to LoadLibrary at the beginning of your program where you" manually "try to load the library"?

can you help me with an example?

the third method has been tried but there is still an error
unit postgres3dyn function InitialisePostgres3 line 7 reads
Code: Pascal  [Select][+][-]
  1. Postgres3LibraryHandle := loadlibrary(libpath);
that is the code that loads the library in memory, the next couple of lines check the correct completion of loading and show a message if failed. Copy them to a button event and add some code that raises the last os error instead. Search for raise last os error to find the appropriate functions in the framework. As far as I can remember libpq.dll uses only 3 or 4 external dlls and except from the msvcrXX.dll all others are part of the system.

If the dll is already in the directory as your exe (not your project but the exe created from your project which usualy is in a different directory in my setup) then the only thing that can fail is that you try to use a 64bit dll from a 32bit application or vice versa.
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on December 09, 2018, 08:29:37 am
Obviously you need to track down the reason why the DLL cannot be loaded.  Following on Marco's idea, I suggest you do the following:

1. add a call to LoadLibrary at the beginning of your program where you "manually" try to load the library.  If LoadLibrary fails then, it is most likely that, as Marco mentioned, one or more of the library dependencies are not found.

2. if the call fails then use a utility such as objdump/pedump/dumpbin to determine the library dependencies and add calls to LoadLibrary for each one of them.  Any one of those that fail will cause your target dll load to fail.

3. You may want to copy those into your program's/executable's folder (to ensure they can be found.) Proceeding this way, you'll find out which dll is causing the problem and lead to a solution.

HTH.

I have got the dependency file from libpq.dll.

but I don't understand how to "add a call to LoadLibrary at the beginning of your program where you" manually "try to load the library"?

can you help me with an example?

the third method has been tried but there is still an error
unit postgres3dyn function InitialisePostgres3 line 7 reads
Code: Pascal  [Select][+][-]
  1. Postgres3LibraryHandle := loadlibrary(libpath);
that is the code that loads the library in memory, the next couple of lines check the correct completion of loading and show a message if failed. Copy them to a button event and add some code that raises the last os error instead. Search for raise last os error to find the appropriate functions in the framework. As far as I can remember libpq.dll uses only 3 or 4 external dlls and except from the msvcrXX.dll all others are part of the system.

If the dll is already in the directory as your exe (not your project but the exe created from your project which usualy is in a different directory in my setup) then the only thing that can fail is that you try to use a 64bit dll from a 32bit application or vice versa.

thank you, I will remember your advice
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: wrubens on May 03, 2019, 09:17:16 am
I had to copy two path's to the windows enviroment settings (path's)

C:\Program Files\PostgreSQL\11\lib 
C:\Program Files\PostgreSQL\11\bin

Then the error was gone.
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: Jonvy on May 13, 2019, 03:13:29 pm
Hello all,

It's not version of Win32 or Win64, also you needn't install Postgres in the PC that running your program.
To connected PostgreSQL, you need the following 5 dll files,
libeay32.dll, libiconv-2.dll,libintl-8.dll,libpq.dll and ssleay32.dll.
You just need copy all these 5 dll files to the same directory as your exe files. Then your program can running
in any windows PC to connect local or server PostgreSQL database.
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: Gebony on May 21, 2019, 02:01:24 pm
add this path "C:\Program Files\PostgreSQL\11\bin"
to the path in environment variables.
Title: Re: Can not load the PostgreSQL client library "libpq.dll". Check your installation
Post by: young_nandy on May 14, 2020, 03:44:49 pm
I have found a solution for this.

download posgresql x32 binaries. then extract and enter the bin folder. take these 4 files:
- libcrypto-1_1.dll
- libiconv-2.dll
- libpq.dll
- libssl-1_1.dll

then copy the 4 files into the application folder

the full version consists of the following files

libcrypto-1_1.dll
libiconv-2.dll
libintl-8.dll
libpq.dll
libssl-1_1.dll
zlib1.dll

Title: Re: Can not load the PostgreSQL client library "libpq.dll" [SOLVED]
Post by: EddeVries on September 09, 2020, 06:50:50 pm
To avoid a lot of guessing:
I installed pgAdmin 4 (in my case the Windows 64-bit version) and checked which dll's this program uses (see C:\Program Files\pgAdmin 4\v4\runtime).
In my case only 4 (not including the Qt- and Python-stuff):
libcrypto-1_1-x64.dll
libpq.dll
libssl-1_1-x64.dll
zlib.dll

I copied these to my application directory and could connect without errors!
I assume the same can be done with the Windows 32-bit version.
TinyPortal © 2005-2018