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 1ntriA-00077D-0b for pgsql-general@arkaria.postgresql.org; Wed, 25 May 2022 14:10:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ntri8-0008J2-46 for pgsql-general@arkaria.postgresql.org; Wed, 25 May 2022 14:10:04 +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 1ntri7-0008Ii-NG for pgsql-general@lists.postgresql.org; Wed, 25 May 2022 14:10:03 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ntrhz-0004M7-Gu for pgsql-general@lists.postgresql.org; Wed, 25 May 2022 14:10:01 +0000 Received: by mail-pj1-x102e.google.com with SMTP id x2-20020a17090a1f8200b001e07a64c461so1745959pja.4 for ; Wed, 25 May 2022 07:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:content-language:to:from :subject; bh=pHjPjiFAKSwXCBort/Jc0j8i2HTHuQAfVB2dgI9U9gg=; b=Nl5wv4zLyhP4nhi+R4Sy/CEmwlRW+QnZIBKHMVXo817bAyqmQSY43W9anxKNZIrB4J SGVFcscdP7KMAUKkbciNuwfwxzBZQYCFWicMksJpzlL45C8OkdX95P+IwZRKYyyHeiBA pBsru3tAGDLia8x/NyVc14pP8aCWBd6a4uuaUfJdDFW8ypvb3/YU8eoz7W646v8tuuBj XD4lXb0Fat1tJzHi3cCAb6M4Q1jPOAmcZPQFnXuq/VfYwocfjnTfjIu5hzIWwve8+TJm 9/JrEX59Ufe6+paRIzvsejrLTk8SYYF3Bouban/QSj/FjuH3DBC8rh1HArUxoGlFlqQM 2QuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:from:subject; bh=pHjPjiFAKSwXCBort/Jc0j8i2HTHuQAfVB2dgI9U9gg=; b=q8HdJf22XurImQH4jYOb4cl1t/YKL9DBycyLXlWvugaYuvaDMYKBX43sXcwGc5wq2D 6h56eOL6gDQvPKY5HtyRbKV0W/xYwAbyrVb40Wa8oigEOipKBPKPtWv47H74/OJu7qte 9qPym8c/4jeqNwBUuJQEMZ5TdVyjTbEWMtocAj5uP6Uct4Y+60yzuS2hvRPJbHfv5yAi 3VO2eREKCUHGRR6gr9FgRLyXkGG0MuIPrFGRi+U0knzH1Hx8Frxz23NJP3BNvONbzvL0 lHmmp8mz/ToHV8Knt7ckqUYCPFOy+H6XW/oS1R2fy6JEtlf1hzf0cfeawssdoRTAQ7yU jiyA== X-Gm-Message-State: AOAM530ceE5oeqEzTH2V4uPIlmgcOMJK1Sbj2i88vWPYPjSSM4wShaEv LPBwEGfUVUThiEQWnoDliQlGGQJCnGQ= X-Google-Smtp-Source: ABdhPJxeZxzhL6YhaxxxiPGUB0rR7cqAhl/cfp0GgjYYYjw40CPkIuaJkH9zER2modD9jCv8ZVhBFw== X-Received: by 2002:a17:90a:fa0c:b0:1e0:a6d1:6b7e with SMTP id cm12-20020a17090afa0c00b001e0a6d16b7emr4238932pjb.209.1653487792755; Wed, 25 May 2022 07:09:52 -0700 (PDT) Received: from [10.128.71.194] ([155.98.131.2]) by smtp.gmail.com with ESMTPSA id n3-20020a170902968300b0015e8d4eb20dsm3758819plp.87.2022.05.25.07.09.51 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 25 May 2022 07:09:52 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------hjrpieJ4FUjEPRnkZHO0hLW1" Message-ID: <3659d499-9a98-6362-7c33-d8f2947474b4@gmail.com> Date: Wed, 25 May 2022 08:09:50 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Content-Language: en-CA To: "pgsql-generallists.postgresql.org" From: Rob Sargent Subject: connect permission based on database name List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------hjrpieJ4FUjEPRnkZHO0hLW1 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Just wondering if I've bumped into some security issue. I'm somewhat surprised that "grant connect to database   to " appears to be stored "by name"? I have the luxury of dropping/recreate databases at will (within limits).  My script for creating a new db also creates a role and grants that role access to that database.  Of course on re-running the script a second time (with same names for db, role) the portion of the script dealing with the role, in its own transaction, fails after the "create role" line.  All that is clearly understood. (And I realize I could clean up the role per drop db.) However, I can still connect to the new database (of same name) using the previously connected role using psql --user role --dbname db.  For the original "grant connect" to still be in play, wouldn't it have to be store using dbname as opposed to an id?  In a different environment, I can see it might be surprising that an "old" role could connect to a "new" database. begin; create schema if not exists study\p\g create role role with login encrypted password 'password'\p\g alter role role set search_path=study,base,public\p\g grant connect on database dbname to role\p\g -- -- Allow this role to diddle with base, bulk and project tables -- grant all on schema base, bulk, study to role\p\g grant all on all tables in schema base, bulk, study to role\p\g commit; --------------hjrpieJ4FUjEPRnkZHO0hLW1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Just wondering if I've bumped into some security issue.

I'm somewhat surprised that "grant connect to database <dbname>  to <role>" appears to be stored "by name"?

I have the luxury of dropping/recreate databases at will (within limits).  My script for creating a new db also creates a role and grants that role access to that database.  Of course on re-running the script a second time (with same names for db, role) the portion of the script dealing with the role, in its own transaction, fails after the "create role" line.  All that is clearly understood. (And I realize I could clean up the role per drop db.)
However, I can still connect to the new database (of same name) using the previously connected role using psql --user role --dbname db.  For the original "grant connect" to still be in play, wouldn't it have to be store using dbname as opposed to an id?  In a different environment, I can see it might be surprising that an "old" role could connect to a "new" database. 

begin;
create schema if not exists study\p\g
create role role with login encrypted password 'password'\p\g
alter role role set search_path=study,base,public\p\g
grant connect on database dbname to role\p\g
--                                                                                                                                                                                                
-- Allow this role to diddle with base, bulk and project tables                                                                                                                                   
--                                                                                                                                                                                                
grant all on schema base, bulk, study to role\p\g
grant all on all tables in schema base, bulk, study to role\p\g
commit;


--------------hjrpieJ4FUjEPRnkZHO0hLW1--