Link to home
Start Free TrialLog in
Avatar of VIkasumit
VIkasumitFlag for India

asked on

Perl and Postgres

Hi,

I need to Create a Postgres Database using Perl Script.

I know how to create Postgres Database using Linux Shell command line but for that I need to login as Postgres user, now when I try to do that in perl script it gives access denied.

Can anyone please let me know how do I create a Postgres database using Perl

Thanks

VIkasumit
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

The most common way is ti use the DBI module and then the driver for your database, DBD::Postgres.  What code do you have so far and where does it fail?
Avatar of VIkasumit

ASKER

Hi,

I am using Shell commands to create the Database, the reason is quite simple that my Perl Installation doesn't have POSTGRES driver and I am not willing to install that. My whole application use that driver just for creating db once. Rest it make User to use phpPgAdmin to manage DB,

anyways, my perl script fails when I do this

#! /usr/bin/perl

$db=`su postgres`; #gives permission denied , Script fails here and hence next line doesn't fire
$db=`createdb xxxxx`; #incomplete line its Postgres Shell script to create DB using psql command

I want that createdb to run as postgres user where as my Perl make it run as root, Also I am not able to authoirze the POSTGRES user i.e. I am not able to login to POSTgre Interface using script.

Hope I am clear

VIkasumit
Okay, see what you want now. I think what you need is to run the postgres command in batch mode from a system call, not interactively as you seem to be.  I don't know the exact postgre command format so will use mysql syntax as an example instead

system("postgres --user='su; --password='pw' -e \"createdb dbase_name\" ");

And then add more sytem calls for each sql statement you want to run.
Hi,

Thanks for let me know what exactly I should ask.... :o) I miss the term "batch command"/"system call" from my  vocabulary .

can you please explain "postgres --user='su; "
and --password='pw'

you mean you aer running postgres with "su" user ?? If thats what you are try than sorry, it will not work as when I try correct command

"psql -Upostgres -Wpassword -c 'CREATE DATABASE dbname';" it give me Authentication failure when run through scripts but if I run it directly on SSH console it works fine

Thanks for you time on question though.

VIkasumit
ASKER CERTIFIED SOLUTION
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial