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 1lHaq8-0000tY-GB for psycopg@arkaria.postgresql.org; Wed, 03 Mar 2021 23:23:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHaq7-0003m1-DF for psycopg@arkaria.postgresql.org; Wed, 03 Mar 2021 23:23:35 +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 1lHaq7-0003lu-7l for psycopg@lists.postgresql.org; Wed, 03 Mar 2021 23:23:35 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHaq4-0005s4-8U for psycopg@postgresql.org; Wed, 03 Mar 2021 23:23:34 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id F05F75C00F9; Wed, 3 Mar 2021 18:23:30 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Wed, 03 Mar 2021 18:23:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=F ZSCvVJs54sBTp5UNhg15tZg1vA+y5qB5pSeIOyBoSU=; b=IWk4I81luReY2ocY2 XL4Z3z4Hmzgu9l2mBr87yRAqHKHNXSWSP1FcdcdFE6SLi42WJ2o7yJER6SmsHrvl ldQjbC3mFPGnTlchRUoBtqA7G2QgnykDFtV4WPygQOn+2ZQ1IkvKtMFzEBZ0b/7k mBtB5FieLhNH12jrH/9J/nrMG/H0Cr8WP7/Ph9OAS+W+cUoJ6YqeKtS/2FPcmCwQ h7WHTj9eZsbVgDSqPmYwoMfuGDHdV1xgZN88PhisrGOePtJy2UCfWM3heR49j1M3 ctWwNYvb1N/Mj2ra3rBig8urm/r5Zs5O8wKirkFvk2VBGMX+BzeS5QZ0C/trYXAA 4rWfA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=FZSCvVJs54sBTp5UNhg15tZg1vA+y5qB5pSeIOyBo SU=; b=lUB8Hz77j6MDVZTAIsN4KeNnCxEr5WBkbEVXMOQczPbSV4kbCUfZy0Xtj 11GH76rNVnTpsJFHl1c0nRk05FOpncIxdrpNCwMCy4tJLED3MAffwURiTsQ7uGnx /eWRZPQiOeEhNuKDsmnDUSE4pPBZlAkh8q4yzJroGTt2Caq8dbLkImuXmCNKzZz4 E/JqPi+ST2H7+MCfDsBffH5AIhzWOxarhT35W+nN1sG3OhRsbPB9FsXgLMMKY+UW psSVEqOncePYOpLe9yrjkqoJ5kD/sVZWIVQOEMpJhxMETSmX3ey+j3gcYYoCXR+R n00kfuaX99MXHt29CLQocYQY20bfw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledruddtfedgtdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpedvleejudetkedtveejteegfeefgeffueffgefhlefg jeehledvgeejhfdttdehfeenucffohhmrghinhepphhoshhtghhrvghsqhhlqdgrrhgthh hivhgvrdhorhhgnecukfhppeehtddrudeljedrkedtrddvnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrg hklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id B36991080063; Wed, 3 Mar 2021 18:23:29 -0500 (EST) Subject: Re: insert DEFAULT value To: Hans Ginzel , psycopg@postgresql.org References: <20210303215137.GI11758@artax.karlin.mff.cuni.cz> From: Adrian Klaver Message-ID: <05f8e563-162a-3a86-d939-7fb7f03811e6@aklaver.com> Date: Wed, 3 Mar 2021 15:23:28 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <20210303215137.GI11758@artax.karlin.mff.cuni.cz> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/3/21 1:51 PM, Hans Ginzel wrote: > How to insert DEFAULT value, please? > > import psycopg2 > from psycopg2.sql import DEFAULT    # > https://www.postgresql-archive.org/Inserting-default-values-into-execute-values-td6130148.html > > > db = psycopg2.connect(host='host', dbname='db') > cursor = db.cursor() > cursor.execute("DROP TABLE IF EXISTS test_default") > cursor.execute("CREATE TABLE test_default(i int NOT NULL DEFAULT 1)") > cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,)) > > cursor.execute("DROP TABLE IF EXISTS test_default") > cursor.execute("CREATE TABLE test_default(j jsonb NOT NULL DEFAULT > '{}'::jsonb)") > cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,)) > > For both cases I get > psycopg2.ProgrammingError: can't adapt type 'SQL' You need to do something like: cur.execute(sql.SQL("INSERT INTO test_default VALUES ({})").format(DEFAULT)) > > Thank you in advance, > HG > > -- Adrian Klaver adrian.klaver@aklaver.com