Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHgmT-00080X-TA for psycopg@arkaria.postgresql.org; Thu, 04 Mar 2021 05:44:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHglv-00050Y-VM for psycopg@arkaria.postgresql.org; Thu, 04 Mar 2021 05:43:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHglv-00050Q-My for psycopg@lists.postgresql.org; Thu, 04 Mar 2021 05:43:39 +0000 Received: from postak.karlin.mff.cuni.cz ([195.113.30.11]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHglo-0000Mw-Kw for psycopg@postgresql.org; Thu, 04 Mar 2021 05:43:38 +0000 Received: from artax.karlin.mff.cuni.cz (artax.karlin.mff.cuni.cz [195.113.26.195]) by postak.karlin.mff.cuni.cz (Postfix) with ESMTPS id 476E0209D2; Thu, 4 Mar 2021 06:43:28 +0100 (CET) Received: by artax.karlin.mff.cuni.cz (Postfix, from userid 1975) id 2DC48828001; Thu, 4 Mar 2021 06:43:28 +0100 (CET) Date: Thu, 4 Mar 2021 06:43:28 +0100 From: Hans Ginzel To: Adrian Klaver Cc: psycopg@postgresql.org Subject: Re: insert DEFAULT value Message-ID: <20210304054327.GL11758@artax.karlin.mff.cuni.cz> References: <20210303215137.GI11758@artax.karlin.mff.cuni.cz> <05f8e563-162a-3a86-d939-7fb7f03811e6@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <05f8e563-162a-3a86-d939-7fb7f03811e6@aklaver.com> User-Agent: Mutt/1.5.21 (2010-09-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Mar 03, 2021 at 03:23:28PM -0800, Adrian Klaver wrote: >On 3/3/21 1:51 PM, Hans Ginzel wrote: >You need to do something like: >cur.execute(sql.SQL("INSERT INTO test_default VALUES ({})").format(DEFAULT)) Formating values into SQL statement is done by the execute(), isn't it? I like the solution from https://www.postgresql-archive.org/Inserting-default-values-into-execute-values-td6130148.html https://www.postgresql-archive.org/Inserting-default-values-into-execute-values-td6130148.html class Default(object): """Set up DEFAULT value for a field. When doing INSERT or UPDATE in Postgres one can use DEFAULT/default as the value to have the server use the default set on the field. """ def __conform__(self, proto): if proto is psycopg2.extensions.ISQLQuote: return self def getquoted(self): return 'DEFAULT' DEFAULT = Default() cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,)) Could this be added to the psycopg2 code, please? H.