Re: bash & postgres

From: "Harvey, Allan AC" <HarveyA(at)OneSteel(dot)com>
To: "Greenhorn" <user(dot)postgresql(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: bash & postgres
Date: 2009-03-23 04:18:13
Message-ID: E97A5BB7699CAD48BE2711E71247116505600829@ntlmsg03.onesteel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org]On Behalf Of Greenhorn
> Sent: Monday, 23 March 2009 3:03 PM
> To: pgsql-general(at)postgresql(dot)org; pgsql-sql(at)postgresql(dot)org
> Subject: [GENERAL] bash & postgres
>
>
> Hi,
>
> I'm trying to pass variables on a bash script embedded with
> psql commands.
>
> cat header.txt
>
> "to1","from1","subject1"
> "to2","from2","subject2"
> "to3","from3","subject3"
> "to4","from4","subject4"
>
> cat b.sh
>
> #!/bin/bash
> two="2"
>
> psql -h localhost -U postgres -d mobile -c "create temp table header (
>
> field_1 text not null,
> field_2 text not null,
> field_3 text not null
>
> );
>
> \\copy header FROM header.txt CSV
>
> SELECT * FROM header limit "$two"; "
>
>
> When I execute b.sh
> ================================
> ERROR: syntax error at or near "\"
> LINE 10: \copy header FROM header.txt CSV
> ^
>
> How do I use \c (or any other psql commands beginning with a "\") in a
> bash script?
>
> Thanks.
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
> #!/bin/bash
> two="2"

Try something like

psql -h localhost -U postgres -d mobile <<ENDOFSQL
create temp table header (

field_1 text not null,
field_2 text not null,
field_3 text not null

);

\copy header FROM header.txt CSV

SELECT * FROM header limit $two;

ENDOFSQL

The material contained in this email may be confidential, privileged or copyrighted. If you are not the intended recipient, use, disclosure or copying of this information is prohibited. If you have received this document in error, please advise the sender and delete the document. Neither OneSteel nor the sender accept responsibility for any viruses contained in this email or any attachments.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Brad Murray 2009-03-23 04:44:58 Time intersect query
Previous Message Greenhorn 2009-03-23 04:03:15 bash & postgres

Browse pgsql-sql by date

  From Date Subject
Next Message DM 2009-03-23 05:44:28 pg_restore error - Any Idea?
Previous Message Greenhorn 2009-03-23 04:03:15 bash & postgres