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 1hT8AZ-0008Bx-LA for pgsql-general@arkaria.postgresql.org; Tue, 21 May 2019 17:03:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hT8AX-0007mk-VO for pgsql-general@arkaria.postgresql.org; Tue, 21 May 2019 17:03:17 +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 1hT8AW-0007hp-M6 for pgsql-general@lists.postgresql.org; Tue, 21 May 2019 17:03:17 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hT8AT-0004kJ-BD for pgsql-general@lists.postgresql.org; Tue, 21 May 2019 17:03:15 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 7620521FED; Tue, 21 May 2019 13:03:12 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 21 May 2019 13:03:12 -0400 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=fm3; bh=3 8tU0wrwZzprZRMHP5dVxFPA4fPvrqyldggzZvqYzeE=; b=SloJjg3JCvVlc7n9w fivClNzYPH4oZsdbHpMB8VMVTtZvpDMRt6SMGq3nFcdXynwWrxSd5mnUvpcZL5ik rP5Iof/0brjJsUMTLVxV2Gd25iB9AMgGzDs75BMjDSmpoITjFwbMeRgMjGKs90a3 NScJVCTOqB1u+kl2nC10X+r0jLMH6+cnPOjQkZ7oTb17EfdW7g0j+ziR5U4wkJe/ ZiRNR2Jss08yz/rWfz5vdlyxx3CoC7GcAT6HXnOakZXSGYrrV9Js1pwuxVmHi5m3 1qiKjq5Rl31ry9L7XW1jpcXrRh1dMYeOYA1Mt8/E4QpFTO9LWzOR5SjPHXAfsuJB /9KuQ== 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=38tU0wrwZzprZRMHP5dVxFPA4fPvrqyldggzZvqYz eE=; b=PDg4IyTJENdf97lJtWi4KwHB7UrYSuT9JI6Q0+FeYi84yE66LsLCEr1An blZC9QiXW3OQzcpyDh1K8J+bsFG+sugPRT2YkjG2zLK9449Pm7HXl3Alw7gy8FJP Q98yt/qONzeP6aMQ3T2SCTxgSyFNoht7m/YbUAsqxU+satWBXdQi8S1OJt3cfOzE poRpuJAVkk3wPitHjbQ5RUsq2X8c8JVwFB6dDprwz4EvMedZ2kmO3BSn5CJS3xV+ SAnIcLMRjOhxvV7bNi6r5ZwnE7D20BPNfhrEzxR45fMgjQ211PtiFQJK8xVA7Hkz US3YYoiprghgvQguliwjM5itClZAQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddutddguddtlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehorhhgrghnihiirghtihhonhhsrdhorhhgpdhpvghophhl vgdrohhrghenucfkphephedtrdduleejrdektddrvdenucfrrghrrghmpehmrghilhhfrh homheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushht vghrufhiiigvpedt 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 83FA58005B; Tue, 21 May 2019 13:03:11 -0400 (EDT) Subject: Re: Bulk inserts into two (related) tables To: Rich Shepard , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Tue, 21 May 2019 10:03:09 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.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 5/21/19 9:56 AM, Rich Shepard wrote: > I'm cleaning and formatting a 800-line data file to be inserted into a > database. Some of the input file fields will be inserted into an > 'organizations' table letting postgres assign sequential org_id numbers. > Other fields will be inserted into a separate 'people' table associated > with > each organization. The people table insert allows postgres to assign the > person_id number and this table's foreign key, people.org_id == > organizations.org_id. > > I could insert all new rows into the organizations table, then produce a > list of the org_id and org_name to manually insert the foreign key in the > related people table. Time and effort intense. > > Is there a way to insert the two tables sequentially without manually > adding > the organizations.org_id to the appropriate foreign key column > (people.org_id) > of the people table? Well you are not going to know the org_id until the organization table is loaded, which means something like: 1) First run through file load the organizations table. 2) Build a mapping of org_id to organization. 3) Run through data file again and load people data using the mapping in 2) to provide the people.org_id. > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com