Is it possible to return the IDs of a multi row insert in SQL?
<p>With the statement below I can insert multiple rows into a table. If the tag exists already then nothing happens, and if it doesn't exist it creates the row.</p>

<pre><code>INSERT INTO tags (name) VALUES ('this'),('is'),('my'),('interest'),('list'),('running'),('pokemongo'),('fighting'),('eating') ON CONFLICT DO NOTHING;
</code></pre>

<p>Is is possible to return the ID of all these values whether or not they exist? </p>

<p>I'm using python psycopg2 with postgres 9.5.</p>

<pre><code>INSERT INTO 
    tags (name) 
VALUES 
    ('this'),('is'),('my'),('interest'),('list'),('running'),('pokemongo'),('fighting'),('eating') 
ON CONFLICT DO NOTHING 
RETURNING ID; # &lt;== this is what you need.
</code></pre>

<p><strong>Edit:</strong>
Right, with this solution you'll only get the ids of inserted rows.</p>

<p>If you need all the ids, you'll have to make another query :</p>

<pre><code>select ID from tags where name in ('this', 'is', 'my', 'interestest', 'list', '...')
</code></pre>

python sql postgresql psycopg2 postgresql-9.5