Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1irltR-0007cR-Q1 for pgsql-general@arkaria.postgresql.org; Wed, 15 Jan 2020 16:51:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1irltQ-0004qo-Il for pgsql-general@arkaria.postgresql.org; Wed, 15 Jan 2020 16:51:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1irltQ-0004mz-5W for pgsql-general@lists.postgresql.org; Wed, 15 Jan 2020 16:51:44 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1irltK-0006X1-BW for pgsql-general@lists.postgresql.org; Wed, 15 Jan 2020 16:51:43 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 78BFA6CE; Wed, 15 Jan 2020 11:51:36 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 15 Jan 2020 11:51:36 -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=M 453G4HXlJfF1Ju9T7Io/20zchm5x/nNmaXme0KyCaA=; b=DrvsSxSH90zW6J83p RqEFk5aYCdjDGBILfqy61sVSMHJ1m3fHHKwSPeyp85GDcu8wBQg3p/R4zGpLWuqr NSpfAYMUCtWDzKufjEf0HmtNicktki/Q2JJ0Ofv6PPyt9VQdSR5O+1KMbM5l/tYh 6Z1M9H4KvQ9Z+QGSTZkZQyw26sZl40w3rk+d8WqOuCU4DPqrHPtgg1cdoRyUx15S Jn4MGuxnotAa6twcuTFtMU7TO3PAL+wGDQ46KSsDY9v+riitu7KBibSd2dhEgWMS 6YZqHaljtyzSeqp6UsGtFCuxTBDih08nnsWEIiPKpj9B2vLyK0teCPj2/ZrCBtDV f6U6g== 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=fm1; bh=M453G4HXlJfF1Ju9T7Io/20zchm5x/nNmaXme0KyC aA=; b=k8I+lmge1/dsM38AWQsli2KHZRxi6g3f4XI7QSMakfZ8LN3Q1EDQrH01T 9ojSgYE3TIBQDNtAxzP4h4SgscQ1mIQJPH/aGhecNenggDl8OtWb3QWEOAMpj/c6 3V7lrtjghAs/OlI9FBytgRHY4FDz+GeieQHo2Cfa43tFB/HkOvUKY3kbEAFT94eC nVVQk9BtVVeJA10S53AOUHAvxDzKME8pULNyYMgdwu1yZwjKQ0VVprhIsarzPmv4 CMSrz0Idt4HfsxaTRZlJHKvsmVjXfFbgKUgqxp3EGCTWTui0XUg3Kr67OhIeDIL1 79c1qWpaughUGbnRe+ceN1M0N/nIQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrtdefgdelvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddvuddv rdduvdelrdduleefnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 5F6B930600A8; Wed, 15 Jan 2020 11:51:35 -0500 (EST) Subject: Re: Problem with SqlState=23505 when inserting rows To: Werner Kuhnle , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <88deb093-c1e2-108d-b9e6-53fddff319c9@aklaver.com> Date: Wed, 15 Jan 2020 08:51:34 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/15/20 8:40 AM, Werner Kuhnle wrote: > > > Hi, > > I'm new to pg and want to port an application and database from ms sql to pg v12. > > Together with my application a database containing initial standard data > which is needed by the application is also installed. > This is done via execution of SQL scripts. > > Every table of the db has an id column and id column values are also contained in the SQL script > which is necessary for consistence. > > In MS SQL id columns were defined as > id INT IDENTITY PRIMARY KEY > > In PG id columns are defined as > id SERIAL PRIMARY KEY > > Importing the SQL script for initial standard data with PG Admin 4 works without any problem. > > But afterwards I get error messages with SqlState=23505 when inserting new rows into that databases by my application. > My application does not provide id values when doing INSERT INTO statements so that new id values are automatically provided by the database. > > This worked in MS SQL DB without any problems. > > But in PG there seem to be conflicts between the row id values of the rows that were > initially imported and the row id values which are automatically provided by the database > if values for id column are missing in INSERT INTO > When automatically providing id values PG seems to ignore the already existing id values. > > I've tried to using the newer definition: > id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY > but that does not solve the problem. > > Is there a way to define id columns to that when the database provides values, > it recognizes already existing values avoiding conflicts. > > Any hint is highly appreciated. My guess is that the SERIAL was defined for the column, then the data was added with id values, but the counter for the sequence behind the SERIAL was not updated to a value greater the the last id added. To verify: 1) In psql do \d some_table and look for the sequence name in the default column e.g: production=# \d order_header Table "public.order_header" Column | Type | Collation | Nullable | Default --------------+--------------------------------+-----------+----------+------------------------------------------------------ order_no | integer | | not null | nextval('order_header_order_no_seq'::text::regclass) So 'order_header_order_no_seq' 2) Then: production=# select * from order_header_order_no_seq; last_value | log_cnt | is_called ------------+---------+----------- 252 | 0 | t (1 row) 3) Then in your case: select max(id) from some_table Postgres also has UPSERT: https://www.postgresql.org/docs/12/sql-insert.html Look for ON CONFLICT Clause Though I don't think this is what you need at this point. > > Regards > > Werner > > > > > -- Adrian Klaver adrian.klaver@aklaver.com