<p>I am trying to execute a command inside postgres container from shell script. This is what I have so far:</p>

<p><code>kubectl exec -it &lt;postgres_pod&gt; -n &lt;deployment&gt; -- bash -c "psql -U postgres -d database -c 'select count from table where name='FOO';'"</code></p>

<p>I am getting the following error:</p>

<p><code>ERROR:  column "foo" does not exist
LINE 1: select count from table where name=FOO;
                                           ^</code></p>

<p>The query runs fine inside the container so there must be something wrong with the way I am passing the command. I did try another query:</p>

<pre><code>kubectl exec -it &lt;postgres_pod&gt; -n &lt;deployment&gt; -- bash -c "psql -U postgres -d database -c 'select * from table;'"
</code></pre>

<p>This runs fine. So, I am guessing that its someting with the way I am passing the where clause <code>where name='FOO'</code>. How can I get this to work. Kindly help me out.</p>

<p><strong>Update:</strong></p>

<p>Tried escaping using:</p>

<p><strong>1: Double Quotes</strong></p>

<p><code>kubectl exec -it &lt;postgres_pod&gt; -n &lt;deployment&gt; -- bash -c "psql -U postgres -d database -c 'select count from table where name=\"FOO\";'"</code></p>

<pre><code>ERROR:  column "FOO" does not exist
LINE 1: select count from table where name="FOO";
                                            ^
</code></pre>

<p><strong>2: Single Quotes</strong></p>

<p><code>kubectl exec -it &lt;postgres_pod&gt; -n &lt;deployment&gt; -- bash -c "psql -U postgres -d database -c 'select count from table where name=\'FOO\';'"</code></p>

<pre><code>bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
command terminated with exit code 1
</code></pre>

<p>I have used <code>$$</code> <strong><a href="https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING" rel="noreferrer">dollar-quoted string</a></strong> in where clause and escaped them using <code>/$</code>.</p>

<pre><code>kubectl exec -it &lt;postgres_pod&gt; -n &lt;deployment&gt; -- bash -c "psql -U postgres -d database -c 'select count from table where name=\$\$FOO\$\$;'"
</code></pre>
