Search code examples
linuxperldbidbd-pg

Can I access Postgre database on another machine using Perl, DBI, and DBD::Pg?


  • My machine: Linux RHEL 5.5.56 (64-bit), Perl 5.8.8. Postgres is not on this machine. But I can ping the Postgres machine no problem. And my machine can obviously install Perl modules from CPAN on the internet.
  • 'uname -a' returns: Linux xxx 2.6.18-371.8.1.el5PAE #1 SMP Fri Mar 28 06:00:03 EDT 2014 i686 i686 i386 GNU/Linux
  • Other machine has Postgres 8.4.
  • I'm new to installing and using any DBI module. Detailed beginner instructions are needed.
  • I have root access to my Linux machine.
  • I think yum reports that I have 'postgresql-libs.i386' installed. `yum install | grep postgres` returns: 'postgresql-libs.i386 8.1.23-10.el5_10 installed'

Is this what I need? This is on my system.

lrwxrwxrwx 1 root root   12 May  1 20:05 /usr/lib/libpq.so.4 -> libpq.so.4.1*
-rwxr-xr-x 1 root root 130K Feb 27 07:36 /usr/lib/libpq.so.4.1*

Goal: I want to grab data from a Postgres server on another machine. I already have a db username, password, IP for the db machine.

Using cpanm I installed DBI just fine, no errors. When I installed DBD::Pg I got an error. CPAN error log follows:

cpanm (App::cpanminus) 1.6941 on perl 5.008008 built for i386-linux-thread-multi
Work directory is /root/.cpanm/work/1405531191.27414
You have make /usr/bin/make
You have LWP 6.04
You have /bin/tar: tar (GNU tar) 1.15.1
You have /usr/bin/unzip
Searching DBD::Pg on cpanmetadb ...
--> Working on DBD::Pg
Fetching http://www.cpan.org/authors/id/T/TU/TURNSTEP/DBD-Pg-3.3.0.tar.gz
-> OK
Unpacking DBD-Pg-3.3.0.tar.gz
Entering DBD-Pg-3.3.0
Checking configure dependencies from META.yml
Checking if you have version 0 ... Yes (0.88)
Checking if you have DBI 1.614 ... Yes (1.631)
Configuring DBD-Pg-3.3.0
Running Makefile.PL
Path to pg_config?
No POSTGRES_HOME defined, cannot find automatically
Configuring DBD::Pg 3.3.0
-> N/A
-> FAIL Configure failed for DBD-Pg-3.3.0. See /root/.cpanm/work/1405531191.27414/build.log for details.
  • Is it even possible for me to get data from the Postgres db on another machine?
  • Does anyone have a link to instructions on how to do this? I've already been googling stuff, reading stuff on CPAN, Stackoverflow and Perlmonks with no results.
  • FAQ at http://dbi.tiddlyspot.com/ has no info specific to installing DBD::Pg.
  • If I install Postgres via yum, will the installation overwrite any files, like libraries? If so, which ones? We do have other applications we use on this machine.

If I get this working, my first step is to show just very basic info about the DBI driver. Next step would be to show a few fields from one table in a SELECT statement.

I won't be returning millions of records, but returning 100 records is certainly in the ballpark.

Thank you!


Solution

  • If you use the package that is already built and available for your system, then you won't have any of these problems.

    $ sudo yum install perl-DBD-Pg
    
    ... Lots of output snipped ...
    
    Dependencies Resolved
    
    ================================================================================
     Package              Arch        Version              Repository          Size
    ================================================================================
    Installing:
     perl-DBD-Pg          x86_64      1.49-4.el5_8         centos-update      115 k
    Installing for dependencies:
     postgresql-libs      x86_64      8.1.23-6.el5_8       centos-update      197 k
    
    Transaction Summary
    ================================================================================
    Install       2 Package(s)
    Upgrade       0 Package(s)
    
    Total download size: 312 k
    Is this ok [y/N]: 
    

    My example uses Centos 5.8.8, but it should work just the same for RHEL.