Re: psql & unix env variables - Mailing list pgsql-general

From Achilleas Mantzios
Subject Re: psql & unix env variables
Date
Msg-id [email protected]
Whole thread Raw
In response to Re: psql & unix env variables  ("Little, Douglas" <[email protected]>)
Responses Re: psql & unix env variables
Re: psql & unix env variables
List pgsql-general
I have found useful the use of variable assignment in psql, e.g.

#!/bin/sh

# lets say you have some var with a value, or even populate some var with a value from
# psql as shown below
somevar=`psql -P pager=off -q -t -c "SELECT foo from bar" | head -1 | sed -e 's/ //g'`

# now use that variable in psql, (what you want to achieve), but in more tight manner
# than simple shell substitution (see -v switch and : notation)

psql -P pager=off -q -v somevar=$somevar -c "select foo2 from bar2 where var=:somevar"

On Τετ 29 Αυγ 2012 15:00:29 Little, Douglas wrote:
> Thanks
>
>
> -----Original Message-----
> From: Ryan Kelly [mailto:[email protected]]
> Sent: Wednesday, August 29, 2012 12:41 PM
> To: Little, Douglas
> Cc: PostgreSQL General ([email protected])
> Subject: Re: [GENERAL] psql & unix env variables
>
> On Wed, Aug 29, 2012 at 12:35:32PM -0500, Little, Douglas wrote:
> > Is there a method for having unix env variables incorporated into a psql sql statement?
> > Ie
> > Export var='dev'
> > Psql =c 'select count(*) from $var.customer;'
> export FOO="bar"
> psql -c "select count(*) from $FOO.customer;"
>
> Note the double quotes. That allows your shell to interpolate the string into your query. Note that the interpolation
isdone by your shell, and not psql. 
>
> >
> >
> >
> > Doug Little
> >
> > Sr. Data Warehouse Architect | Business Intelligence Architecture |
> > Orbitz Worldwide
> > 500 W. Madison, Suite 1000  Chicago IL 60661| Office 312.260.2588 |
> > Fax 312.894.5164 | Cell 847-997-5741 [email protected]<mailto:[email protected]>
> >  [cid:[email protected]]   orbitz.com<http://www.orbitz.com/> | ebookers.com<http://www.ebookers.com/>
|hotelclub.com<http://www.hotelclub.com/> | cheaptickets.com<http://www.cheaptickets.com/> |
ratestogo.com<http://www.ratestogo.com/>| asiahotels.com<http://www.asiahotels.com/> 
> >
>
> -Ryan Kelly
>
>
>
-
Achilleas Mantzios
IT DEPT


pgsql-general by date:

Previous
From: Dmitriy Igrishin
Date:
Subject: Re: PQfformat question and retrieving bytea data in C
Next
From: Grzegorz Tańczyk
Date:
Subject: Refreshing functional index