Re: How to surround a selected value with double quotes?

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: "Subramanian,Ramachandran" <ramachandran(dot)subramanian(at)alte-leipziger(dot)de>, "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: How to surround a selected value with double quotes?
Date: 2026-06-22 08:26:24
Message-ID: 4c3c231f54e9d6bae4cba0c84079f000901da653.camel@cybertec.at
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Mon, 2026-06-22 at 07:46 +0000, Subramanian,Ramachandran wrote:
> I wrote a script to analyze and run vacuum on tables that need it.

I assume you are talking about a bash script.

> Sadly at the time I wrote the script, I never expected a schema name to be pure numbers. 
>  
> So my Script does not work now.
>  
> I want to add double quote marks to each schema name and table name select.
> I tried using the CONCAT function, but it does not work as I expected it to.
>  
> I would be grateful if someone can help me understand the mistake I am making with the CONCAT.

You are a victim of (self-inflicted) SQL injection.

Just surrounding the schema name with double quotes is not enough.
What if the schema name itself contains a double quote?

You should use PostgreSQL's functions to properly quote an identifier.
With a shell script, I think your only choice is to use SQL:

#!/bin/bash

schema='12345 43'
table='tab"567'

quoted_schema=$(psql -Atq -v var="$schema" <<EOF
SELECT quote_ident(:'var');
EOF
)

quoted_table=$(psql -Atq -v var="$table" <<EOF
SELECT quote_ident(:'var');
EOF
)

sql="SELECT col FROM $quoted_schema.$quoted_table"

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Subramanian,Ramachandran 2026-06-22 08:55:48 AW: How to surround a selected value with double quotes?
Previous Message Subramanian,Ramachandran 2026-06-22 07:46:09 How to surround a selected value with double quotes?