Perl and Postgres


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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
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?
VIkasumitAuthor Commented:

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

Kim RyanIT ConsultantCommented:
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.
VIkasumitAuthor Commented:

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.

Kim RyanIT ConsultantCommented:
yes you need the one line form. Does this work from perl?
system("psql -Upostgres -Wpassword -c 'CREATE DATABASE dbname;' ");
If it runs from console it should run in Perl if you are using the same host.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.