Thread: connecting in shell scripts ??

connecting in shell scripts ??

From
"Anand Raman"
Date:
hi guys
I wish to connect to the database thru a cron job and do some sql queries,,
However i am having problems in connecting to the database thru the shell
script.. I am unable to pass the password..
I have been trying to do the following

#! /bin/sh
psql -h localhost db1 -U foo -c "select current_timestamp" < passwdfile
where passwdfile has the passwd for the connection

This isnt working.. I am a little confused for the same thing worked for
pg_dump..

Looking forward for ur suggestions..
I am on 7.0.3

Thanx
Anand Raman


Re: connecting in shell scripts ??

From
Alvaro Herrera
Date:
On Thu, 4 Jan 2001, Anand Raman wrote:

> hi guys
> I wish to connect to the database thru a cron job and do some sql queries,,
> However i am having problems in connecting to the database thru the shell
> script.. I am unable to pass the password..
> I have been trying to do the following
>
> #! /bin/sh
> psql -h localhost db1 -U foo -c "select current_timestamp" < passwdfile
> where passwdfile has the passwd for the connection

How'bout something like

#!/bin/sh
USER=foo
DBNAME=db1
PASS=password_for_foo
psql -U $USER $DBNAME << EOF
$PASS
SELECT current_timestamp;
EOF

When processing for output, you have to check for the prompt
"Password:", though. Maybe there's a better way...

--
Alvaro Herrera (<alvherre[@]protecne.cl>)


Re: connecting in shell scripts ??

From
Larry Rosenman
Date:
* Alvaro Herrera <alvherre@protecne.cl> [010104 13:32]:
> On Thu, 4 Jan 2001, Anand Raman wrote:
>
> > hi guys
> > I wish to connect to the database thru a cron job and do some sql queries,,
> > However i am having problems in connecting to the database thru the shell
> > script.. I am unable to pass the password..
> > I have been trying to do the following
> >
> > #! /bin/sh
> > psql -h localhost db1 -U foo -c "select current_timestamp" < passwdfile
> > where passwdfile has the passwd for the connection
>
> How'bout something like
>
> #!/bin/sh
> USER=foo
> DBNAME=db1
> PASS=password_for_foo
> psql -U $USER $DBNAME << EOF
> $PASS
> SELECT current_timestamp;
> EOF
psql (actually libpq) will use:

PGUSER for username
PGPASSWORD for password
if they are in the environment.

Larry
>
> When processing for output, you have to check for the prompt
> "Password:", though. Maybe there's a better way...
>
> --
> Alvaro Herrera (<alvherre[@]protecne.cl>)

--
Larry Rosenman                     http://www.lerctr.org/~ler
Phone: +1 972-414-9812                 E-Mail: ler@lerctr.org
US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749