<p>is there an alternative for <code>mysql_insert_id()</code> php function for PostgreSQL? Most of the frameworks are solving the problem partially by finding the current value of the sequence used in the ID. However, there are times that the primary key is not a serial column....</p>

[{'answer_id': 55959, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 2, 'selected': False, 'text': '<p>Check out the <a href="http://www.postgresql.org/docs/current/interactive/sql-insert.html" rel="nofollow noreferrer">RETURNING optional clause</a> for an INSERT statement. (Link to official PostgreSQL documentation)</p>\n\n<p>But basically, you do:</p>\n\n<pre><code>INSERT INTO table (col1, col2) VALUES (1, 2) RETURNING pkey_col\n</code></pre>\n\n<p>and the INSERT statement itself returns the id (or whatever expression you specify) of the affected row.</p>\n'}, {'answer_id': 56055, 'author': 'Vertigo', 'author_id': 5468, 'author_profile': 'https://Stackoverflow.com/users/5468', 'pm_score': 2, 'selected': False, 'text': '<p>From php.net:</p>\n\n<pre><code>$res=pg_query("SELECT nextval(\'foo_key_seq\') as key");\n$row=pg_fetch_array($res, 0);\n$key=$row[\'key\'];\n// now we have the serial value in $key, let\'s do the insert\npg_query("INSERT INTO foo (key, foo) VALUES ($key, \'blah blah\')");\n</code></pre>\n\n<p>This should always provide unique key, because key retrieved from database will be never retrieved again.</p>\n'}, {'answer_id': 56133, 'author': 'angch', 'author_id': 5386, 'author_profile': 'https://Stackoverflow.com/users/5386', 'pm_score': 6, 'selected': True, 'text': '<p>From the PostgreSQL point of view, in pseudo-code:</p>\n\n<pre><code> * $insert_id = INSERT...RETURNING foo_id;-- only works for PostgreSQL &gt;= 8.2. \n\n * INSERT...; $insert_id = SELECT lastval(); -- works for PostgreSQL &gt;= 8.1\n\n * $insert_id = SELECT nextval(\'foo_seq\'); INSERT INTO table (foo...) values ($insert_id...) for older PostgreSQL (and newer PostgreSQL)\n</code></pre>\n\n<p><code>pg_last_oid()</code> only works where you have OIDs. OIDs have been off by default since PostgreSQL 8.1.</p>\n\n<p>So, depending on which PostgreSQL version you have, you should pick one of the above method. Ideally, of course, use a database abstraction library which abstracts away the above. Otherwise, in low level code, it looks like:</p>\n\n<h1>Method one: INSERT... RETURNING</h1>\n\n<pre><code>// yes, we\'re not using pg_insert()\n$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");\n$insert_row = pg_fetch_row($result);\n$insert_id = $insert_row[0];\n</code></pre>\n\n<h1>Method two: INSERT; lastval()</h1>\n\n<pre><code>$result = pg_execute($db, "INSERT INTO foo (bar) values (123);");\n$insert_query = pg_query("SELECT lastval();");\n$insert_row = pg_fetch_row($insert_query);\n$insert_id = $insert_row[0];\n</code></pre>\n\n<h1>Method three: nextval(); INSERT</h1>\n\n<pre><code>$insert_query = pg_query($db, "SELECT nextval(\'foo_seq\');");\n$insert_row = pg_fetch_row($insert_query);\n$insert_id = $insert_row[0];\n$result = pg_execute($db, "INSERT INTO foo (foo_id, bar) VALUES ($insert_id, 123);");\n</code></pre>\n\n<p>The safest bet would be the third method, but it\'s unwieldy. The cleanest is the first, but you\'d need to run a recent PostgreSQL. Most db abstraction libraries don\'t yet use the first method though.</p>\n'}, {'answer_id': 5863620, 'author': 'Jenaro', 'author_id': 634605, 'author_profile': 'https://Stackoverflow.com/users/634605', 'pm_score': 2, 'selected': False, 'text': '<p>You also can use:</p>\n\n<pre><code>$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");\n$insert_row = pg_fetch_result($result, 0, \'foo_id\');\n</code></pre>\n\n<p>You have to specify in pg_fetch_result the number of the row and the name of the field that you are looking for, this is a more precise way to get the data that you need, but I don\'t know if this has some penalty in the performance of the query. Remember that this method is for PostgreSQL versions 8.2 and up.</p>\n'}]
['https://Stackoverflow.com/questions/55956', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5742/']