Lazarus

Programming => Databases => Topic started by: tbonejo on November 21, 2011, 02:46:03 pm

Title: Postgres Cannot connect
Post by: tbonejo on November 21, 2011, 02:46:03 pm
Hi,

I am trying to connect to a postgres db on Mac OSX Snow Leopard. When I set connection to true in the design part it says it cannot find libpq.dylib. It is installed correctly and I can use the db with PgAdmin but i am at a loss as to why it will not find the correct files to load the db.

Any Ideas on how to fix this?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 21, 2011, 04:00:37 pm
I am assuming there is a way to set the path to where the postgres libs reside so that it is known to lazarus or something but am not sure where that would be set? I did add the path to my compiler options but that doesnt seem to work either.
Title: Re: Postgres Cannot connect
Post by: ludob on November 21, 2011, 04:23:40 pm
The DYLD_LIBRARY_PATH environment variable is used to find libraries. on Mac OSX.
In Lazarus go to Run / Run Parameters / Environment tab and check or add the environment variable.
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 21, 2011, 05:22:49 pm
I added these items but that doesnt seem to work either.

Any other ideas as to what I would need to do?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 21, 2011, 09:42:12 pm
Does anyone have any ideas as to resolve this? I just need to simply connect to the postgre server, it should'nt be this hard should it?
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 21, 2011, 10:56:07 pm
Is there a way to set the path to which the libs are for the connection?
Title: Re: Postgres Cannot connect
Post by: cpalx on November 22, 2011, 02:36:29 am
install pgadmin3, then search the file libpq.dylib and copy in /usr/lib

works perfect for me.

I use zeoslib and i had to do something else. simbolic link libpq.so -> libpq.dylib
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 12:06:50 pm
So I should be able to put the dylib link in usr/lib? Also how do you set a symbolic link in osx like you were saying?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 12:11:44 pm
I just tried copying in that file to usr/lib and that didnt work either. I am assuming I need to set a symbolic link then. I did see in the unit it looks for .so instead of the dylib.
Title: Re: Postgres Cannot connect
Post by: BigChimp on November 22, 2011, 02:04:17 pm
Don't know if such a tool exists for OSX, but for Solaris/Linux you have the strace tool, that can show all file access - might help if you're totally stuck otherwise

This http://stackoverflow.com/questions/3007868/how-can-get-dtrace-to-run-the-traced-command-with-non-root-priviledges (http://stackoverflow.com/questions/3007868/how-can-get-dtrace-to-run-the-traced-command-with-non-root-priviledges)) seems to say something like:
Code: [Select]
sudo dtruss -f su -l whoami cd pwd && <insertlazarusstartupcommandhere>
Title: Re: Postgres Cannot connect
Post by: cpalx on November 22, 2011, 02:13:38 pm

sudo -i   # <write your password>
cd /usr/lib
ln libpq.dylib  libpq.so

that works fine for me.
Title: Re: Postgres Cannot connect
Post by: BigChimp on November 22, 2011, 02:19:58 pm
If that works, anybody else think a patch to make the postgresql connector work for OSX is in order?!?!?  :)
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 02:32:51 pm
Man I must be having lousy luck cause that didnt work for me either.

Should I be using the zeos db stuff rather than pqconnection?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 06:35:55 pm
Quote
ln libpq.dylib  libpq.so
I had to do a
Code: [Select]
sudo ln -s /Applications/pqAdmin3.app/Contents/Frameworks/libpq.5.dylib /usr/lib/libpq.dylib
sudo ln -s /usr/lib/libpq.dylib /usr/lib/libpq.so
to get TPQConnection working.  pqAdmin3 is installed in the  /Applications directory. A dtruss showed that the program first opens libpq.dylib and then libpq.so  :o
Quote
If that works, anybody else think a patch to make the postgresql connector work for OSX is in order?!?!?  :)
I'm re-building fpc with debug info to find where the libpq.so is called. But yes, this has all the looks of a bug.
Quote
Should I be using the zeos db stuff rather than pqconnection?
Zeos doesn't support OSX :(
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 06:49:56 pm
Ok so I tried that but the really odd part is that my pgadmin3 does not have a frameworks folder in it? I can't link to that app cause I do not have anything in the contents like you show here.

What version of Postgres are you using?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 07:06:09 pm
Quote
What version of Postgres are you using?
pgAdmin v1.14 from http://www.pgadmin.org/download/macosx.php (http://www.pgadmin.org/download/macosx.php). The Postgres  database is on another server and the version is not relevant to this problem. On OSX I only have pgAdmin3.
To find the location of the libpq library used by pgAdmin run
Code: [Select]
otool -L /path to pgA2dmin3/pgAdmin3.App/COntents/MacOS/pgAdmin3 | grep libpqI found the bug in TPQConnection. In packages\postgres\src\dllistdyn.pp the library is hard coded to libpq.so. I'll create a bug report with a patch.
In the mean time, creating the symlink libpq.so solves this problem.
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 07:08:48 pm
Awesome! Is this an easy fix I can apply to my code base now?

I downloaded the new version and did the link stuff but that didnt seem to do much for fixing my issue? I noticed that if I dbl click the file in the /usr/lib/ it says the alias is broken, but I assume it shouldnt be?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 07:14:21 pm
Using that cmd to find what lib pgadmin uses gives me this:


Tom-Russells-MacBook-Pro:~ Tom$ otool -L /path to pgA2dmin3/pgAdmin3.App/COntents/MacOS/pgAdmin3 | grep libpq
otool: can't open file: /path (No such file or directory)
otool: can't open file: to (No such file or directory)
otool: can't open file: pgA2dmin3/pgAdmin3.App/COntents/MacOS/pgAdmin3 (No such file or directory)

Does this mean my install is bad or something?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 07:21:02 pm
Quote
Does this mean my install is bad or something?
No. That means that you have to replace "/path to pgA2dmin3/" with the path where you installed pgAdmin3. For me this is /Applications/pgAdmin3.App/COntents/MacOS/pgAdmin3.
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 07:33:12 pm
At least I got some output:


/Applications/pgAdmin3.app/Contents/MacOS/pgAdmin3 (architecture i386):
   /System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos (compatibility version 5.0.0, current version 5.0.0)
   /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
   /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 128.0.0)
   /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 11.0.0)
   /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.3.11)
   /System/Library/Frameworks/QuickTime.framework/Versions/A/QuickTime (compatibility version 1.0.0, current version 1327.73.0)
   /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
   /System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_stc-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_richtext-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_aui-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_xrc-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_qa-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_html-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_adv-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_core-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_base_carbonu_xml-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_base_carbonu_net-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_base_carbonu-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libxml2.2.dylib (compatibility version 10.0.0, current version 10.1.0)
   /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.3)
   /usr/lib/libiconv.2.dylib (compatibility version 5.0.0, current version 5.0.0)
   @executable_path/../../Contents/Frameworks/libxslt.1.dylib (compatibility version 3.0.0, current version 3.23.0)
   @executable_path/../../Contents/Frameworks/libpq.5.dylib (compatibility version 5.0.0, current version 5.4.0)
   /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
   /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Applications/pgAdmin3.app/Contents/MacOS/pgAdmin3 (architecture ppc):
   /System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos (compatibility version 5.0.0, current version 5.0.0)
   /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
   /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 128.0.0)
   /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 11.0.0)
   /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.3.11)
   /System/Library/Frameworks/QuickTime.framework/Versions/A/QuickTime (compatibility version 1.0.0, current version 1327.73.0)
   /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
   /System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_stc-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_richtext-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_aui-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_xrc-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_qa-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_html-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_adv-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_macu_core-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_base_carbonu_xml-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_base_carbonu_net-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libwx_base_carbonu-2.8.0.dylib (compatibility version 9.0.0, current version 9.0.0)
   @executable_path/../../Contents/Frameworks/libxml2.2.dylib (compatibility version 10.0.0, current version 10.1.0)
   /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.3)
   /usr/lib/libiconv.2.dylib (compatibility version 5.0.0, current version 5.0.0)
   @executable_path/../../Contents/Frameworks/libxslt.1.dylib (compatibility version 3.0.0, current version 3.23.0)
   @executable_path/../../Contents/Frameworks/libpq.5.dylib (compatibility version 5.0.0, current version 5.4.0)
   /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0)
   /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 07:39:59 pm
Good   :)
Quote
/Applications/pgAdmin3.app/Contents/MacOS/pgAdmin3
Your pgAdmin3 is installed in the same location as on my machine.
Quote
@executable_path/../../Contents/Frameworks/libpq.5.dylib
and libpq.5.dylib  is in the same place. This means you can run the 2 lines I gave you before:
Code: [Select]
sudo ln -s /Applications/pqAdmin3.app/Contents/Frameworks/libpq.5.dylib /usr/lib/libpq.dylib
sudo ln -s /usr/lib/libpq.dylib /usr/lib/libpq.so
This should solve your problem.
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 07:44:09 pm
Something is amiss for me cause those 2 lines I just tried and it still says that it cannot find the libpq.dylib.

I am trying to connect it via design time though.
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 07:49:19 pm
What is
Code: [Select]
ls -l /usr/lib/libpq.*returning?
Quote
I am trying to connect it via design time though.
That is fine. For me that works perfect.
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 08:37:08 pm
lrwxr-xr-x  1 root  wheel  60 Nov 22 13:42 /usr/lib/libpq.dylib -> /Applications/pqAdmin3.app/Contents/Frameworks/libpq.5.dylib
lrwxr-xr-x  1 root  wheel  20 Nov 22 13:42 /usr/lib/libpq.so -> /usr/lib/libpq.dylib


Thats what it returns.
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 09:20:39 pm
That is what it should be.
So there is something else missing or wrong. In the mean time, I guess you have tried already a lot of things. Possibly there is some conflicting stuff now.
Can you do the following:
In lazarus, start new application, drop Tpqconnection on the form, in form.oncreate enter
Code: [Select]
PQConnection1.connected:=true;, save as project1, build, run. It will say "libpq.dylib not found". In a terminal window, cd to the directory containing your new project1 and run
Code: [Select]
sudo dtruss project1.app/Contents/MacOS/project1 &> dtruss.out Click terminate on the window "libpq.dylib not found". This will create the file dtruss.out in your current directory. If you are courageous, open the file in an editor and look for libpq.dylib. It will show the different paths used to find the library and, if it finds the library, show the dependencies it tries to open. If you prefer you can also attach the file to your next post  ;)
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 09:35:54 pm
This is the main part of the output that pertains to my issue:

stat64("libpq.dylib\0", 0xBFFFEC4C, 0x3)       = -1 Err#2
stat64("/Users/Tom/lib/libpq.dylib\0", 0xBFFFF03C, 0x3)       = -1 Err#2
stat64("/usr/local/lib/libpq.dylib\0", 0xBFFFF03C, 0x3)       = -1 Err#2
stat64("/usr/lib/libpq.dylib\0", 0xBFFFF03C, 0x3)       = -1 Err#2

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 09:56:53 pm
AHA! Now we are getting somewhere. It looks like you are trying to use a 32 bit dylib with a 64 bit program.
Do
Code: [Select]
file /usr/lib/libpq.dylib
file project1.app/Contents/MacOS/project1
Probably you get Mach-O executable i386 for the first and Mach-O 64-bit for the second.
If that is the case compile for 32 bit or use a 64 bit libpq.dylib. There is a 64 bit client available here http://www.kyngchaos.com/software:postgres (http://www.kyngchaos.com/software:postgres)

Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 10:09:15 pm
Weird but this is what I get:


file /usr/lib/libpq.dylib
/usr/lib/libpq.dylib: broken symbolic link to /Applications/pqAdmin3.app/Contents/Frameworks/libpq.5.dylib
Tom-Russells-MacBook-Pro:LazTestNate Tom$ file project1.app/Contents/MacOS/project1
project1.app/Contents/MacOS/project1: Mach-O executable i386



Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 10:36:28 pm
Quote
sudo ln -s /Applications/pqAdmin3.app/Contents/Frameworks/libpq.5.dylib /usr/lib/libpq.dylib
There is a typo. This should be
Code: [Select]
sudo ln -s /Applications/pgAdmin3.app/Contents/Frameworks/libpq.5.dylib /usr/lib/libpq.dylibpgAdmin3 instead of pqAdmin3  %)
That explains your broken link.
Title: Re: Postgres Cannot connect
Post by: tbonejo on November 22, 2011, 10:54:36 pm
OMG, that works. Finally it works!!! Thanks sooooo much.

SO at least this works for now until they accept the patch for the one file then?

Thanks,

Tom
Title: Re: Postgres Cannot connect
Post by: ludob on November 22, 2011, 11:02:53 pm
Quote
SO at least this works for now until they accept the patch for the one file then?
Yes. The libpq.so symlink is needed until the patch is applied. From a functional point there is no difference.

You're welcome.