pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Edson Richter <edsonrichter@hotmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: How to double-quote a double quoted identifier?
Date: Mon, 15 Jun 2020 21:24:40 -0400
Message-ID: <1270458.1592270680@sss.pgh.pa.us> (raw)
In-Reply-To: <DM6PR13MB39880CD8E721A9A0C23EB76FCF9D0@DM6PR13MB3988.namprd13.prod.outlook.com>
References: <DM6PR13MB39880CD8E721A9A0C23EB76FCF9D0@DM6PR13MB3988.namprd13.prod.outlook.com>

Edson Richter <edsonrichter@hotmail.com> writes:
> How do I remote execute que following command:
> CREATE EXTENSION "uuid-ossp"
>     SCHEMA public
>     VERSION "1.1";

> I'm using PostgreSQL 9.6, Linux x64, and bash.

> I've tried the following:

> ssh -l postgres -2 -C -p 2022 192.168.0.70 "psql -h 127.0.0.1 -d test_db -p 5432 -c \"CREATE EXTENSION \\\\""uuid-ossp\\\\"" SCHEMA pg_catalog;\" "

Yeah, nesting quoting levels in shell is kind of a bear.

> I know this is a kind of "bash-psql" issue, but I would appreciate if anyone could share experience with this.

Since you're using bash, you can get bash to do the work for you,
via multiple iterations of the ${variable@Q} construct.

$ SQLCMD='CREATE EXTENSION "uuid-ossp" SCHEMA pg_catalog;'
$ echo $SQLCMD
CREATE EXTENSION "uuid-ossp" SCHEMA pg_catalog;
$ echo ${SQLCMD@Q}
'CREATE EXTENSION "uuid-ossp" SCHEMA pg_catalog;'
-- not too exciting so far, but wrap it into another layer of quoting:
$ SHCMD="psql -h 127.0.0.1 -d test_db -p 5432 -c ${SQLCMD@Q}"
$ echo $SHCMD
psql -h 127.0.0.1 -d test_db -p 5432 -c 'CREATE EXTENSION "uuid-ossp" SCHEMA pg_catalog;'
$ echo ${SHCMD@Q}
'psql -h 127.0.0.1 -d test_db -p 5432 -c '\''CREATE EXTENSION "uuid-ossp" SCHEMA pg_catalog;'\'''
-- and now your answer is:
$ echo ssh -l postgres -2 -C -p 2022 192.168.0.70 ${SHCMD@Q}
ssh -l postgres -2 -C -p 2022 192.168.0.70 'psql -h 127.0.0.1 -d test_db -p 5432 -c '\''CREATE EXTENSION "uuid-ossp" SCHEMA pg_catalog;'\'''

You could, of course, just use the end result of that -- but it's
probably better to have a shell script recalculate it on the fly
given the desired SQL command as input.

			regards, tom lane





view thread (3+ messages)  latest in thread

Message-ID: <1270458.1592270680@sss.pgh.pa.us>
Permalink:  ../1270458.1592270680@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1270458.1592270680@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, edsonrichter@hotmail.com
  Subject: Re: How to double-quote a double quoted identifier?
  In-Reply-To: <1270458.1592270680@sss.pgh.pa.us>
  References: <DM6PR13MB39880CD8E721A9A0C23EB76FCF9D0@DM6PR13MB3988.namprd13.prod.outlook.com> <1270458.1592270680@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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