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 1mX6WL-0002IZ-Bb for pgsql-general@arkaria.postgresql.org; Sun, 03 Oct 2021 18:47:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mX6WK-000309-4A for pgsql-general@arkaria.postgresql.org; Sun, 03 Oct 2021 18:47:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mX6WJ-000301-RO for pgsql-general@lists.postgresql.org; Sun, 03 Oct 2021 18:47:31 +0000 Received: from mail.netnea.com ([82.220.26.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mX6WI-000682-8Z for pgsql-general@lists.postgresql.org; Sun, 03 Oct 2021 18:47:31 +0000 X-Virus-Scanned: Debian amavisd-new at Subject: Re: PostgreSQL CHECK Constraint To: Shaozhong SHI Cc: pgsql-general References: <42aaef2a-6f55-0449-5a35-d6507d333af1@netnea.com> From: Christian Ramseyer Message-ID: Date: Sun, 3 Oct 2021 20:47:28 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > The reported error looks like this: > > postgres@dellstore ERROR: new row for relation "test_customers" > violates check constraint "check_age" .. > > This errors appears in the serverlog which has many format and > forwarding options, you can read about them here: On 03.10.21 20:16, Shaozhong SHI wrote: > Hi, Christian, > That is interesting.  Can errors be captured and saved as data with > scripting? Yes that works quite the same, e.g. in Python you can do try: cur.execute("insert into test_customers (firstname, lastname, age) values ( %s, %s, %s)", ("Bobby", "Tables", 10)) except psycopg2.errors.CheckViolation as e: print(f"That didn't work: {e.cursor.query} failed") print(f"{e.pgerror}") HTH