Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 22B452E0084 for ; Fri, 11 Apr 2008 14:41:51 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 26934-04-3 for ; Fri, 11 Apr 2008 14:41:46 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ag-out-0708.google.com (ag-out-0708.google.com [72.14.246.240]) by developer.postgresql.org (Postfix) with ESMTP id 0718B2E0078 for ; Fri, 11 Apr 2008 14:41:45 -0300 (ADT) Received: by ag-out-0708.google.com with SMTP id 23so1174974agd.5 for ; Fri, 11 Apr 2008 10:41:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=A0vP2SGsymBzn3Bvy32xd+Ussg4VRAxa+0lyeSNn23M=; b=HXTgbDXM1j2uHwdnTvBlNusDXO7ISom/CTS3u/n6Ig8E9eL/25ieRiEGjkjiLFMYGir+qIxpWVobkTWAW4go4zQKUlsD4dOuF6nJsO3DuWYoIjIcvXepHT1T5xT/KdkyQC+nuPUNg/LiSW7HOyLr8CcIrjYieQDDCYjy33Nz4EM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=r4r9J5Uhl1/jyYCKo8JUmOj5kHaOIL/QCXdvNoWhZ7tMPyRB5voWLBBo6qRRG0j6w8d4UwnnpG1IfvDDlaSceJZx9h9zPc6TX9MUKRH/kJUj938lmtdKfPI3WkEhEMpbwB92ZD/B292Cc3hTZ3zy7jFJJc8A3Q3UdLGQFU4AzMs= Received: by 10.114.13.1 with SMTP id 1mr3871177wam.4.1207935701362; Fri, 11 Apr 2008 10:41:41 -0700 (PDT) Received: by 10.114.137.7 with HTTP; Fri, 11 Apr 2008 10:41:41 -0700 (PDT) Message-ID: Date: Fri, 11 Apr 2008 11:41:41 -0600 From: "Scott Marlowe" To: "Nacef LABIDI" Subject: Re: Get the last inserted id Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/77 X-Sequence-Number: 30616 On Fri, Apr 11, 2008 at 11:23 AM, Nacef LABIDI wrote: > Hi all, > > I was wondering if postgres can return the last ID inserted for a table > which the primary key is autoincremented. I need to retrieve this ID in my > code to continue processing on that inserted row. smarlowe=# create table test (a serial primary key, b text); NOTICE: CREATE TABLE will create implicit sequence "test_a_seq" for serial column "test.a" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE smarlowe=# insert into test (a,b) values (DEFAULT,'test') returning a; a --- 1 (1 row) INSERT 0 1 Note that there are other ways of doing it, but I really like the returning clause.