Recent

Author Topic: Can not load the PostgreSQL client library "libpq.dll" [SOLVED]  (Read 27968 times)

young_nandy

  • New Member
  • *
  • Posts: 41
Can not load the PostgreSQL client library "libpq.dll" [SOLVED]
« 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?
« Last Edit: May 16, 2020, 01:10:49 am by young_nandy »

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
If your application is 64bit then it cannot work with 32bit .dll (or vice versa).

young_nandy

  • New Member
  • *
  • Posts: 41
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?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
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)

young_nandy

  • New Member
  • *
  • Posts: 41
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

440bx

  • Hero Member
  • *****
  • Posts: 3946
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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

young_nandy

  • New Member
  • *
  • Posts: 41
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?

440bx

  • Hero Member
  • *****
  • Posts: 3946
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

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.



« Last Edit: December 09, 2018, 03:14:26 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

young_nandy

  • New Member
  • *
  • Posts: 41
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

dinmil

  • New Member
  • *
  • Posts: 45
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

young_nandy

  • New Member
  • *
  • Posts: 41
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.

taazz

  • Hero Member
  • *****
  • Posts: 5368
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.
« Last Edit: December 09, 2018, 08:10:41 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

young_nandy

  • New Member
  • *
  • Posts: 41
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

wrubens

  • Newbie
  • Posts: 1
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.

Jonvy

  • Jr. Member
  • **
  • Posts: 90
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.

 

TinyPortal © 2005-2018