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 1q7FeO-0006fi-KW for pgsql-admin@arkaria.postgresql.org; Thu, 08 Jun 2023 13:26:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q7FeN-0008FH-Er for pgsql-admin@arkaria.postgresql.org; Thu, 08 Jun 2023 13:26:03 +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 1q7FeN-0008F8-3i for pgsql-admin@lists.postgresql.org; Thu, 08 Jun 2023 13:26:03 +0000 Received: from mout-u-107.mailbox.org ([80.241.59.207]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q7FeK-0011n9-AD for pgsql-admin@lists.postgresql.org; Thu, 08 Jun 2023 13:26:02 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4QcQ2X34jWz9sSb; Thu, 8 Jun 2023 15:25:56 +0200 (CEST) Date: Thu, 8 Jun 2023 15:25:55 +0200 (CEST) From: Erik Wienhold To: Jorge Silva , "pgsql-admin@lists.postgresql.org" Message-ID: <1940899558.103110.1686230755873@office.mailbox.org> In-Reply-To: References: Subject: Re: Allow an user (not superuser) to create a testing database from a production database MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4QcQ2X34jWz9sSb List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 07/06/2023 23:19 CEST Jorge Silva wrote: > > We have a database which recurrently we create a testing database making = a > copy of it. Under the superuser =E2=80=9Cpostgres=E2=80=9D we use the com= mand > =E2=80=9CCREATE DATABASE totvs_test WITH TEMPLATE totvs_production;=E2=80= =9D which works > pretty well. How do you deal with existing connections to totvs_production? Connections= to the template database are not possible during CREATE DATABASE and existing connections must be closed beforehand. That alone would be a dealbreaker f= or me given that it's a production database. But I don't know your requiremen= ts on availability. > But now I would like to allow an user, which is not the owner of the > totvs_production database, and neither is a superuser, to execute this sa= me > command. Is it a requirement to create the database via SQL? The same can be achiev= ed on the command line with pg_dump and pg_restore which are designed for copy= ing databases without preventing other database connections. =09createdb totvs_test && pg_dump -Fc totvs_production | pg_restore -d totv= s_test --single-transaction --verbose > In order to achieve this, it seems that I have to give him the access > =E2=80=9CCREATEDB=E2=80=9D and make our production database a template da= tabase. Yes. > Do you know if there any safety, performance or any other kind of issue i= n > making our production database as a template in postgres and keep using i= t > for production tasks? Autovacuum uses a different default freeze age for template databases [0]. So the global vacuum setting does not apply to template databases. But I d= on't know how that affects performance. Also I would not expect a live database to be flagged as a template databas= e. > This user, who would make this copy, already has access to SELECT in all = the > data of this database, but they have no rights in DELETE, UPDATE or INSER= T. The docs[1] on templates databases don't say anything about access privileg= es besides the CREATEDB and SUPERUSER privileges that you already mentioned. = You have to test that and see if the new database contains everything. The access privileges definitely affect what the user can dump with pg_dump= . That's why pg_dump is usually run as superuser to make a full copy. I neve= r tested it with a user that doesn't have full access. [0] https://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dblob;f=3Dsrc/= backend/postmaster/autovacuum.c;h=3Df929b62e8adaa4219694c2c74648c623d7e9f68= 8;hb=3D378d73ef204d0dcbeab834d52478e8cb90578ab7#l2054 [1] https://www.postgresql.org/docs/current/manage-ag-templatedbs.html -- Erik