Re: " " around fields with psql

Lists: pgsql-general
From: Steve Clark <sclark(at)netwolves(dot)com>
To: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: " " around fields with psql
Date: 2012-02-10 18:26:23
Message-ID: 4F35614F.4030706@netwolves.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello,

Is there a way with psql to get column output to be
"data1","data2",...,"datan"

I tried -F "," but that left off the first and last quote.

I can't seem to find a way in the man page.

Thanks,
--
Stephen Clark
*NetWolves*
Director of Technology
Phone: 813-579-3200
Fax: 813-882-0209
Email: steve(dot)clark(at)netwolves(dot)com
http://www.netwolves.com


From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Steve Clark <sclark(at)netwolves(dot)com>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: " " around fields with psql
Date: 2012-02-10 19:12:52
Message-ID: CAOR=d=2VmiHCZVsPL5WZYtY=taz=tzNch+3a5bqAGC7XQ1z-dg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Feb 10, 2012 at 11:26 AM, Steve Clark <sclark(at)netwolves(dot)com> wrote:
> Hello,
>
> Is there a way with psql to get column output to be
> "data1","data2",...,"datan"
>
> I tried -F "," but that left off the first and last quote.
>
> I can't seem to find a way in the man page.

Well, you can do it yourself kinda like this:

select '""||field1||'", "||field2||'" from sometable where yada.


From: Steve Clark <sclark(at)netwolves(dot)com>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: " " around fields with psql
Date: 2012-02-10 19:33:05
Message-ID: 4F3570F1.3090903@netwolves.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 02/10/2012 02:12 PM, Scott Marlowe wrote:
> On Fri, Feb 10, 2012 at 11:26 AM, Steve Clark<sclark(at)netwolves(dot)com> wrote:
>> Hello,
>>
>> Is there a way with psql to get column output to be
>> "data1","data2",...,"datan"
>>
>> I tried -F "," but that left off the first and last quote.
>>
>> I can't seem to find a way in the man page.
> Well, you can do it yourself kinda like this:
>
> select '""||field1||'", "||field2||'" from sometable where yada.
>
Ok that will work

Thanks,

--
Stephen Clark
*NetWolves*
Director of Technology
Phone: 813-579-3200
Fax: 813-882-0209
Email: steve(dot)clark(at)netwolves(dot)com
http://www.netwolves.com


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Steve Clark <sclark(at)netwolves(dot)com>
Cc: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>, pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: " " around fields with psql
Date: 2012-02-10 22:30:27
Message-ID: CAHyXU0wJ3d70zTP31Yr2POataMj21dev9S4qWDBo+paK1W7soA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Feb 10, 2012 at 1:33 PM, Steve Clark <sclark(at)netwolves(dot)com> wrote:
> On 02/10/2012 02:12 PM, Scott Marlowe wrote:
>
> On Fri, Feb 10, 2012 at 11:26 AM, Steve Clark <sclark(at)netwolves(dot)com> wrote:
>
> Hello,
>
> Is there a way with psql to get column output to be
> "data1","data2",...,"datan"
>
> I tried -F "," but that left off the first and last quote.
>
> I can't seem to find a way in the man page.
>
> Well, you can do it yourself kinda like this:
>
> select '""||field1||'", "||field2||'" from sometable where yada.
>
> Ok that will work

for 9.1+ you can use built in format() function for a lot of fields:
select format('"%s", "%s", "%s", "%s"', procpid, usename, waiting,
query_start) from pg_stat_activity;

also with recent postgres you can use hstore to convert virtually any
query as such:
select '"' || array_to_string(avals(hstore(a)), '", "') || '"' from
pg_stat_activity a;

postgres=# select '"' || array_to_string(avals(hstore(q)), '", "') ||
'"' from (select 1 as a,2 as b,3 as c) q;
?column?
---------------
"1", "2", "3"

etc.
merlin


From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: " " around fields with psql
Date: 2012-02-11 08:03:56
Message-ID: jh57dc$lrh$1@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 2012-02-10, Steve Clark <sclark(at)netwolves(dot)com> wrote:

> Is there a way with psql to get column output to be
> "data1","data2",...,"datan"

assuming you are trying to be compatible with CSV:

copy ( your_query_here ) to stdout with csv header ;

--
⚂⚃ 100% natural


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: " " around fields with psql
Date: 2012-02-13 19:13:37
Message-ID: CAHyXU0xoWKDyd8L8==3wtk1LXYpcbX8L8UDHtzfX47-gAx1DMg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sat, Feb 11, 2012 at 2:03 AM, Jasen Betts <jasen(at)xnet(dot)co(dot)nz> wrote:
> On 2012-02-10, Steve Clark <sclark(at)netwolves(dot)com> wrote:
>
>> Is there a way with psql to get column output to be
>> "data1","data2",...,"datan"
>
> assuming you are trying to be compatible with CSV:
>
>  copy ( your_query_here ) to stdout with csv header ;

yeah -- that's the best way if you want actual csv, from psql you'd
probably want to do \copy:
postgres=# \copy (select 1, '"', 'ab,c') to stdout csv header;
?column?,?column?,?column?
1,"""","ab,c"

note that per csv rules columns are only required to be quoted to
protect from unambiguous parsing. also, double quotes in your field
will be escaped.

merlin


From: Steve Clark <sclark(at)netwolves(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>, pgsql-general(at)postgresql(dot)org
Subject: Re: " " around fields with psql
Date: 2012-02-13 20:43:34
Message-ID: 4F3975F6.4090702@netwolves.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 02/13/2012 02:13 PM, Merlin Moncure wrote:
> On Sat, Feb 11, 2012 at 2:03 AM, Jasen Betts<jasen(at)xnet(dot)co(dot)nz> wrote:
>> On 2012-02-10, Steve Clark<sclark(at)netwolves(dot)com> wrote:
>>
>>> Is there a way with psql to get column output to be
>>> "data1","data2",...,"datan"
>> assuming you are trying to be compatible with CSV:
>>
>> copy ( your_query_here ) to stdout with csv header ;
> yeah -- that's the best way if you want actual csv, from psql you'd
> probably want to do \copy:
> postgres=# \copy (select 1, '"', 'ab,c') to stdout csv header;
> ?column?,?column?,?column?
> 1,"""","ab,c"
>
> note that per csv rules columns are only required to be quoted to
> protect from unambiguous parsing. also, double quotes in your field
> will be escaped.
>
> merlin
>
Thanks to all that replied.

--
Stephen Clark
*NetWolves*
Director of Technology
Phone: 813-579-3200
Fax: 813-882-0209
Email: steve(dot)clark(at)netwolves(dot)com
http://www.netwolves.com