Re: psql syntax for array of strings in a variable?

From: Philip Semanchuk <philip(at)americanefficient(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: psql syntax for array of strings in a variable?
Date: 2021-10-29 18:44:15
Message-ID: 7EBBD1CA-CE01-4026-BEB5-73064D4B07B2@americanefficient.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On Oct 29, 2021, at 2:05 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> "David G. Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> writes:
>> On Friday, October 29, 2021, Philip Semanchuk <philip(at)americanefficient(dot)com>
>> wrote:
>>> I would appreciate help with the syntax for querying an array of strings
>>> declared as a psql variable. Here's an example.
>>>
>>> \set important_days ARRAY['monday', 'friday']
>
>> Not sure why the single quotes are getting stripped out but that is the
>> issue. Maybe double them up to escape them like in a normal text literal?
>
> Yeah, that's just the way that \set works (and most other psql backslash
> commands, I believe). You've likely got an issue with whitespace
> disappearing, too, though that might be harmless in this specific example.
>
> regression=# \set foo 'bar baz'
> regression=# \echo :foo
> bar baz
> regression=# \set foo 'bar ''baz'
> regression=# \echo :foo
> bar 'baz
> regression=# \set foo bar ''baz
> regression=# \echo :foo
> barbaz
>
> Not sure offhand how well-documented this is.

Thanks, all. Glad to know I wasn’t missing something obvious.

> On Oct 29, 2021, at 1:52 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:

> psql variables can hold only text. There is not any type - all is just text.

^^^ This was especially helpful; I’d never considered that before.

Cheers
Philip

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Westermann (DWE) 2021-10-30 08:55:34 Why does the OID jump by 3 when creating tables?
Previous Message Tom Lane 2021-10-29 18:05:24 Re: psql syntax for array of strings in a variable?