Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evfBf-0002k6-86 for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 08:21:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evfBd-0004Hp-2u for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 08:21:33 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evfBc-0004Hf-La for pgsql-general@lists.postgresql.org; Tue, 13 Mar 2018 08:21:32 +0000 Received: from mail-wr0-x22a.google.com ([2a00:1450:400c:c0c::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1evfBV-0002jn-3v for pgsql-general@postgresql.org; Tue, 13 Mar 2018 08:21:31 +0000 Received: by mail-wr0-x22a.google.com with SMTP id s18so5987698wrg.9 for ; Tue, 13 Mar 2018 01:21:24 -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 :content-transfer-encoding:message-id:references:to; bh=xqeAl1/HH4BcIqcvx7MHqhCM4AQ4rXOKvoCBvFhJR98=; b=o6T5USFobZGopSeZdOeG3xC/pB5cP00dA1XYs/QrhejXs+uJ+c+Ldf+6FFzJFsRY+r LSXulCzGJIwvs9qJ6j4gGpqEREdLd29uEq21D2f+YmxgSH5Gvx/6UW/CWnkLTPDUisHX Ki4gDWDWlPbo4Vfynpiw3CJH9ZmHNkshhaKvgRYEg7/1T09fSZkJ/kMdMkspyNewIIx6 r5YAMeCuFNHGoEzJ1gEMBa8T3JpINANZHAg8l6nLbkDOTNaT9w4dKn+WAUZIS2fgfReq VuMTx92c/sFvVsxq6O4Utkxi39786L3ERvbCGcW0di3+L0YSPp66FPQWZgoH6kmKqA2E gtZg== 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 :content-transfer-encoding:message-id:references:to; bh=xqeAl1/HH4BcIqcvx7MHqhCM4AQ4rXOKvoCBvFhJR98=; b=JYiwcn/TxMZJjJcsuCtahF9KOgRtY7u3XtObya/+NQVsKN19lT1GWiWeoU3itsXs9C tGTs3vT0AwdbNB1OZGmkbSWz5CdWQ1ilkoeGM3zgnaehU1fsB6hojWmOuNyUKjaRAH6b S9RV6A/HEhO+UzJsV7TWylSB4B0IBTsz3KhcBq6WlHizynzcOgtoBB6V44IF6CHrCcUC ZWWx0Mbwxh78XYIkNvuAo9Yl2GUCQtDtaUWPCw9tB7f0qkYSDf53OJcJJIJ08PaW5h+k IwyUB3R5BwMKPt6lM59gPL04vnhUUtiHVC/dM0M959LXztQjZ+fgQS7MyZ/keKss08k5 gOdg== X-Gm-Message-State: AElRT7EUg4lHRVqgKCwh6BSRTMSyjD2IoC4HtqVCMO73Hrn4pkimrcYG Kv2N0YthrltiPMxpRV6skl4= X-Google-Smtp-Source: AG47ELtNyEymDv8XNezcEby07IA5xlElAHk7zhCRbm5PpOhltu/XwSbh5Pc47pws0s/FApzQNVC1wg== X-Received: by 10.80.244.178 with SMTP id s47mr14405085edm.106.1520929282192; Tue, 13 Mar 2018 01:21:22 -0700 (PDT) Received: from hollewijn.internal (84-245-13-9.dsl.cambrium.nl. [84.245.13.9]) by smtp.gmail.com with ESMTPSA id t16sm6103115edt.68.2018.03.13.01.21.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Mar 2018 01:21:21 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 11.2 \(3445.5.20\)) Subject: Re: Programmatically duplicating a schema From: Alban Hertroys In-Reply-To: Date: Tue, 13 Mar 2018 09:21:19 +0100 Cc: pgsql-general@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: matt.figg@internode.on.net X-Mailer: Apple Mail (2.3445.5.20) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 13 Mar 2018, at 4:23, matt.figg@internode.on.net wrote: >=20 > Hi all, >=20 > What is a reliable way to programmatically & generically populate an = empty schema with all the objects in the public schema as a template? >=20 > We are using the multi tenancy ruby gem Apartment ( = https://github.com/influitive/apartment ), which was recently broken by = the changes made to pg_dump to address CVE-2018-1058 = https://nvd.nist.gov/vuln/detail/CVE-2018-1058 >=20 > Apartment attempts to duplicate the public schema whenever creating a = new schema by running: >=20 > pg_dump -s -x -0 -n public >=20 > to get the SQL statements needed to recreate the public schema & then = executes the pg_dump's sql output after creating & switching to the new = schema ( via set search_path to ; ) >=20 > After the fix to CVE-2018-1058, all table references in pg_dump's = output (including within SQL of stored procedures) are prefixed by the = public. schema, which means you cannot just reuse this output in a = different schema context without first manually changing the sql. > As a temporary fix so we can handle new customers in production, we = are using a regex search/replace for public. in the pg_dump output, but = clearly this is not a reliable solution for a generic gem such as = Apartment. In my opinion, it makes sense that if you have the option of dumping the = contents of a specific schema, it should be possible to restore that = dump into a different schema. Unfortunately, looking at pg_restore, = there does not appear to be such an option (yet). I'd even go so far to suggest that every single object type that can be = dumped with pg_dump (single database, single schema, single table, = single function, etc) should be restorable under a different name. I = realise that this could make pg_restore options potentially more = confusing. I suppose people currently manually edit the dumps to this effect, but = that risks silent corruption of data when for example a data value = contains a string such as 'The building is now open to public.'. Regular = expressions don't know the difference between data and identifiers in a = dump file - pg_restore does. Whether psql needs the same treatment? I'd qualify this as "advanced" = use and limit it to pg_restore. But then, I'm just a list-lurker, I currently have but the option of = voicing my opinion. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.