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 1hT9Tq-0003Yu-SK for pgsql-general@arkaria.postgresql.org; Tue, 21 May 2019 18:27:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hT9Tp-0008JR-FL for pgsql-general@arkaria.postgresql.org; Tue, 21 May 2019 18:27:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hT9Tp-0008JC-1G for pgsql-general@lists.postgresql.org; Tue, 21 May 2019 18:27:17 +0000 Received: from mail-it1-x12a.google.com ([2607:f8b0:4864:20::12a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hT9Tm-0006OU-TL for pgsql-general@lists.postgresql.org; Tue, 21 May 2019 18:27:16 +0000 Received: by mail-it1-x12a.google.com with SMTP id i63so6245091ita.3 for ; Tue, 21 May 2019 11:27:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc:message-id:references :to; bh=n4VPL4bOOw8bInXHp/78l0y8bLVC/cGr3zpb9kcP6uI=; b=fm2QyIR3oCnPEsomebbacBY/wOsJChOr8TySLe+Bi7GXCn8hcS6Bp80BEVdJCK6ias R/VcSpcqHsWEJ8pfLwrp/GlaF7JLmPvQ6zqYEOZSi5u5W678bYdRdONp1y5MqGt9fI3o voo4bImGVGpzRF5nIt3q/N7skCqbnGJbRZ42Ym4k0XsGV6B3L0KlWObaxBhiPfA0pA06 1OkLc4Qre3XquVWuEZkGMi8eAuFfVl/w4BL58DZ/d4X/jGJ7VTrndrh0QZ1UGegbEpIT r8gPWRW4mZR77xo9wE/mH3YJNIVdCm54DshMYJ9C92rbS/5susIyeopogZNG2jxYDewN IrxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to; bh=n4VPL4bOOw8bInXHp/78l0y8bLVC/cGr3zpb9kcP6uI=; b=LCFgcyJy0wuLCdZ11LMuwcI8PNYYY0OXP9T9toMBfYNVBa1dGJw5h3EKlJMiZwkuSq plt575R14JVArUYZFAVVWhV+NWkSToV7W+ewZfB82jBj2vdLpMwursFxwKXFHcCfywXA HaS5QY2oUsRH1k0C3J2XcjRWezH/48+4khPrNeI4FrL178dNfLcMLIezhYq3kdlx/8+x LfuRDZ1s+cYywizKShOyGbChM45TIcAFpcuwlJqXcsuzS+3JiCfzJsgSd1JzqYvdAwLe auQAol27e4Uyocb+DzgqhXUw+skmIBylxj/vqOdsaNRV+0yEaRuXgSbbZsjEOhUzrhQn W2IQ== X-Gm-Message-State: APjAAAUxeq0q7GYYcqe78khAHl+N7Uc+ETzrDd3kfhRuEgJpIde6J4ZC Z5fU47O1CqjJv7QM/L9v345xnN0y X-Google-Smtp-Source: APXvYqwyvGzW8kOG1DzbqI9vFxAzAItyTISwXm2KFiVwe39U5zGW6Iy7O6UHRur443b1PBOgKlJc4A== X-Received: by 2002:a02:3449:: with SMTP id z9mr15937724jaz.120.1558463233312; Tue, 21 May 2019 11:27:13 -0700 (PDT) Received: from [10.35.60.64] ([64.164.57.170]) by smtp.gmail.com with ESMTPSA id t191sm1738383itb.20.2019.05.21.11.27.12 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 21 May 2019 11:27:12 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_CC3E7DEF-5D8A-4294-9F67-FE0DA4665018" Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) Subject: Re: Bulk inserts into two (related) tables From: Will Hartung In-Reply-To: Date: Tue, 21 May 2019 11:27:10 -0700 Cc: pgsql-general@lists.postgresql.org Message-Id: References: To: Rich Shepard X-Mailer: Apple Mail (2.3124) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_CC3E7DEF-5D8A-4294-9F67-FE0DA4665018 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On May 21, 2019, at 9:56 AM, Rich Shepard = wrote: >=20 > 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. You can either use a script for the entire process, or, convert the = people table to INSERT statements that have a SELECT for the foreign key = as part of the insert. INSERT INTO PEOPLE (id, name, org_id) VALUES (1, =E2=80=98Bob=E2=80=99, = (SELECT org_id FROM org WHERE org_name=3D=E2=80=98Main Office=E2=80=99)) --Apple-Mail=_CC3E7DEF-5D8A-4294-9F67-FE0DA4665018 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On May 21, 2019, at 9:56 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote:

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.

You can either = use a script for the entire process, or, convert the people table to = INSERT statements that have a SELECT for the foreign key as part of the = insert.

INSERT INTO PEOPLE (id, = name, org_id) VALUES (1, =E2=80=98Bob=E2=80=99, (SELECT org_id FROM org = WHERE org_name=3D=E2=80=98Main Office=E2=80=99))


= --Apple-Mail=_CC3E7DEF-5D8A-4294-9F67-FE0DA4665018--