<p>I want to connect to psql using password on <code>kubectl exec</code> command on kubernetes like </p>

<pre class="lang-none prettyprint-override"><code>kubectl exec -it postgres -- bash \`psql -h $IP -U admin --password password -p $PORT dbname\`
</code></pre>

<p>I tried to command </p>

<pre class="lang-none prettyprint-override"><code>kubectl exec -it $podid -- bash \`psql -h $IP -U admin --password -p $PORT postgresdb\`
</code></pre>

<p>and </p>

<pre class="lang-none prettyprint-override"><code>kubectl exec -it $podid -- bash -c "psql -- dbname=postgresql://postgres:password@$ip:$port/postgresdb -c 'INSERT INTO public."user" (user_id,user_pw,user_nm,email,phone,is_admin) VALUES ('admin','admin','admin','admin@admin.com',NULL,true);'" 
</code></pre>

<p>but these command did not work.</p>

<p>How can I connect to psql using kubernetes commands and password?</p>

<p>try<br>
<code>kubectl exec -it &lt;postgresspod-name&gt; bash or sh</code><br>
when you inside the container you can just do<br>
<code>PGPASSWORD=&lt;password&gt; psql -h &lt;host or service name or localhost&gt; -U &lt;username&gt; &lt;dbname&gt;</code></p>

<p>another option is
<code>kubectl exec -it postgres -- bash \`PGPASSWORD=&lt;password&gt; psql -h &lt;host or service name or localhost&gt; -U &lt;username&gt; &lt;dbname&gt;\`</code></p>
