pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Passing parameters
4+ messages / 4 participants
[nested] [flat]

* Passing parameters
@ 2016-03-22 14:44  Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>
  0 siblings, 3 replies; 4+ messages in thread

From: Ferrell, Denise D CTR NSWCDD, H11 @ 2016-03-22 14:44 UTC (permalink / raw)
  To: pgsql-admin

Hi All,

Using PostgreSQL v9.3 on Linux Red Hat...

I've got a script that I'm running from the Linux command prompt that I need to pass 2 parameter's into the psql script without using the VI Editor.  Is there an easy way to accomplish this?

psql <database> -f <scriptname> -l <log_file>



Thank you in advance.
Denise

Attachments:

  smime.p7s (application/pkcs7-signature, 5.5K) download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Passing parameters
@ 2016-03-22 14:58  Stephen Frost <sfrost@snowman.net>
  parent: Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>
  2 siblings, 0 replies; 4+ messages in thread

From: Stephen Frost @ 2016-03-22 14:58 UTC (permalink / raw)
  To: Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>; +Cc: pgsql-admin

Hi Denise!

* Ferrell, Denise D CTR NSWCDD, H11 (denise.ferrell.ctr@navy.mil) wrote:
> Using PostgreSQL v9.3 on Linux Red Hat...
> 
> I've got a script that I'm running from the Linux command prompt that I need to pass 2 parameter's into the psql script without using the VI Editor.  Is there an easy way to accomplish this?
> 
> psql <database> -f <scriptname> -l <log_file>

It really depends on how you want to use the variables in the script.

psql has a '-v' option which you can use to set variables that can be
used with the ':varname' syntax inside of the script, for example:

psql -v table=abc -f script.sql

Inside of script.sql you can then have ':table', like so:

select * from :table;

and that ':table' will be replaced by 'abc' when psql is run with the
'-v table=abc' parameter.

Hope that helps!

Thanks!

Stephen

Attachments:

  signature.asc (application/pgp-signature, 818B) download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Passing parameters
@ 2016-03-22 15:01  Scott Whitney <scott@journyx.com>
  parent: Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>
  2 siblings, 0 replies; 4+ messages in thread

From: Scott Whitney @ 2016-03-22 15:01 UTC (permalink / raw)
  To: Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>; pgsql-admin

What's wrong with doing it like this, for example?

#!/bin/bash
user=$1
database=$2
table=$3
myfield=$4
logfile=$5
mysql="select $4 from $3;"
psql -U $user $database -c "mysql" -l "$5"




Then:
/tmp/myscript postgres mydb mytable myfield /tmp/mylog



________________________________________
From: pgsql-admin-owner@postgresql.org <pgsql-admin-owner@postgresql.org> on behalf of Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>
Sent: Tuesday, March 22, 2016 9:44 AM
To: pgsql-admin@postgresql.org
Subject: [ADMIN] Passing parameters

Hi All,

Using PostgreSQL v9.3 on Linux Red Hat...

I've got a script that I'm running from the Linux command prompt that I need to pass 2 parameter's into the psql script without using the VI Editor.  Is there an easy way to accomplish this?

psql <database> -f <scriptname> -l <log_file>



Thank you in advance.
Denise


Journyx, Inc.
7600 Burnet Road #300
Austin, TX 78757
www.journyx.com

p 512.834.8888
f 512-834-8858

Do you receive our promotional emails? You can subscribe or unsubscribe to those emails at http://go.journyx.com/emailPreference/e/4932/714/


-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Passing parameters
@ 2016-03-22 15:07  John DeSoi <desoi@pgedit.com>
  parent: Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>
  2 siblings, 0 replies; 4+ messages in thread

From: John DeSoi @ 2016-03-22 15:07 UTC (permalink / raw)
  To: Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>; +Cc: pgsql-admin


> On Mar 22, 2016, at 9:44 AM, Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil> wrote:
> 
> I've got a script that I'm running from the Linux command prompt that I need to pass 2 parameter's into the psql script without using the VI Editor.  Is there an easy way to accomplish this?
> 
> psql <database> -f <scriptname> -l <log_file>

See the psql command line variable options (-v, --set, --variable).

http://www.postgresql.org/docs/9.5/static/app-psql.html#APP-PSQL-VARIABLES

John DeSoi, Ph.D.



-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2016-03-22 15:07 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-03-22 14:44 Passing parameters Ferrell, Denise D CTR NSWCDD, H11 <denise.ferrell.ctr@navy.mil>
2016-03-22 14:58 ` Stephen Frost <sfrost@snowman.net>
2016-03-22 15:01 ` Scott Whitney <scott@journyx.com>
2016-03-22 15:07 ` John DeSoi <desoi@pgedit.com>

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