pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rudi Starcevic <rudi@oasis.net.au>
To: pgsql-sql@postgresql.org
Subject: Re: psql and password
Date: Thu, 28 Mar 2002 15:06:41 +1000
Message-ID: <3CA2A4E1.1040607@oasis.net.au> (raw)
References: <3CA27648.7060307@oasis.net.au>

Hi,

Thanks for the suggestions,

I ended up using Perl and DBI.
First I create a new database,
then create a new user,
then change owership of the database to the new user,
then create and entry for Apache's htpasswd facility.

Then the new user logs into the new database with there new htpasswd 
account via phpPgAdmin.
Thanks again.
For those interested here's my script.

Cheers
Rudi.

Script :
#!/usr/bin/perl -w

use DBI;

$DBH = DBI ->connect("dbi:Pg:dbname=oasis;host=moon","postgres","rudi2000");


foreach $_ (@ARGV)
{
    # get name and pwd
    @data = split(/:/,$_);
    $thisname = $data[0];
    $thispwd  = $data[1];
   
    # create a new user
    $sql = $DBH->prepare("CREATE USER $thisname WITH PASSWORD '$thispwd' 
NOCREATEDB NOCREATEUSER;");
    $sql->execute();

    # create a new database
    $sql = $DBH->prepare("CREATE DATABASE $thisname WITH 
TEMPLATE=template0;");
    $sql->execute();

    # change database ownership
    $sql = $DBH->prepare("UPDATE pg_database SET datdba = (SELECT 
usesysid FROM pg_shadow WHERE usename = '$thisname') WHERE datname = 
'$thisname';");
    $sql->execute();
   
    # create htpaswd account
    system("/usr/local/apache/bin/htpasswd -b /home/realmz/phpPgAdmin 
$thisname $thispwd >> /root/scripts/htpasswd_log.txt 2>&1");
}

$DBH->disconnect;






Rudi Starcevic wrote:

> Hi,
>
> I'm wondering how I can create a database for a user from within a 
> script.
> I have Pg in 'password' mode.
> To create this database I'm trying to pass in the password from the 
> script.
> For example :
>
> echo myPgPassword | /usr/local/pgslq/bin/createdb yourNewPgDatabase
>
> But it's not working.
> I'm still prompted for the password.
>
> My goal is to create the database then change ownership of the database.
> The user can then log straight in using phpPgAdmin.
>
> Can you see where I'm going wrong ?
> Any suggestions ?
> Perhaps I need to use 'Expect' or something ( this is a Debian box )






view thread (3+ messages)  latest in thread

Message-ID: <3CA2A4E1.1040607@oasis.net.au>
Permalink:  ../3CA2A4E1.1040607@oasis.net.au/
Also on:    postgresql.org/message-id/3CA2A4E1.1040607@oasis.net.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rudi@oasis.net.au
  Subject: Re: psql and password
  In-Reply-To: <3CA2A4E1.1040607@oasis.net.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox