<p>In Kubernetes, I want start a PostgreSQL pod, create database, user and grant all privileges to the database to the user. So I came up with the following command:</p>
<pre><code>kubectl run somepod --rm -i --restart=Never --image postgres:alpine --tty -- psql &quot;postgresql://postgres:somepass@localhost&quot; -c &quot;CREATE DATABASE testdb&quot; -c &quot;CREATE USER myUser WITH PASSWORD 'password'&quot; -c &quot;GRANT ALL PRIVILEGES ON DATABASE testdb TO myUser&quot;
</code></pre>
<p>It already works, but unfortunately the pod isn't terminated after the command. In Kubernetes, the pod is still alive and it's status is &quot;Succeeded&quot;. I already tried adding <code>-c '\q'</code> to the command line because I assumed that psql is still running, but it didn't helped. Any ideas how I can remove the pod after the command has been succeeded?</p>

Nrgyzer
<p>You can try something like this:</p>
<pre><code>kubectl run  test4 -i --tty --image=postgres --restart=Never --rm  --command -- echo &quot;hello&quot;
hello
pod &quot;test4&quot; deleted
</code></pre>
<p>In your example, you are just executing a command in a running container, where postgres is started already.</p>
<p>You need to overvrite the command which is executed when the container is started.</p>
<p>Executing your command: (the error comes only because i didnt install any postgres)</p>
<p>kubectl run  test4 -i --tty --image=postgres --restart=Never --rm  --command -- psql &quot;postgresql://postgres:somepass@localhost&quot; -c &quot;CREATE DATABASE testdb&quot; -c &quot;CREATE USER myUser WITH PASSWORD 'password'&quot; -c &quot;GRANT ALL PRIVILEGES ON DATABASE testdb TO myUser&quot;</p>
<p>This will work if your postgres on localhost has right credentials.</p>
<p>Or it will return like so, because connection couldnt be established:</p>
<pre><code>kubectl run  test4 -i --tty --image=postgres --restart=Never --rm  --command -- psql &quot;postgresql://postgres:somepass@localhost&quot; -c &quot;CREATE DATABASE testdb&quot; -c &quot;CREATE USER myUser WITH PASSWORD 'password'&quot; -c &quot;GRANT ALL PRIVILEGES ON DATABASE testdb TO myUser&quot;
psql: error: connection to server at &quot;localhost&quot; (127.0.0.1), port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?
connection to server at &quot;localhost&quot; (::1), port 5432 failed: Cannot assign requested address
        Is the server running on that host and accepting TCP/IP connections?
pod &quot;test4&quot; deleted
</code></pre>
<p>But however the pod is beeing deleted.</p>
<p>Hint to not doing that always manually , you can put a initContainer in your deployment or execute a job. Both with same image and Command.</p>
