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 1iFZC1-0000bo-4U for pgsql-general@arkaria.postgresql.org; Wed, 02 Oct 2019 07:37:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iFZBz-0002s6-Sd for pgsql-general@arkaria.postgresql.org; Wed, 02 Oct 2019 07:36:59 +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 1iFZBz-0002rd-Fa for pgsql-general@lists.postgresql.org; Wed, 02 Oct 2019 07:36:59 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iFZBw-0005J0-Pj for pgsql-general@lists.postgresql.org; Wed, 02 Oct 2019 07:36:58 +0000 Received: by mail-wr1-x444.google.com with SMTP id r5so18314645wrm.12 for ; Wed, 02 Oct 2019 00:36:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=Gian5uqoo2smFFIQzvd/1FzUwi87vdbk/rqem/OITc8=; b=weiYlIGZRWQMZufLSgw9T51IXER+yUkpxbVe4F1WWRHfkGq7kPmFk2TUN+YzWJe7Lr nFxGYRyGxZYSFfS8vx8ncNC25wuCZOQAC6gqn6NwQXERuKpsHA/fIiubcSCGwSCq86ij tRYrSDQQTIazNEw7sS8LZJyUyoolkn1zT457YfUTOgD/N4YlHp0nmBcv7mmpGCDjBH1r mPmp710ta60G8sUhVjgrTIjCeHnlPmLjoQw1oVjaY1jtTjNN0sbx+v6HEa1gHkCAznok O+zG+MSoU0Ehz3q5ivmxOrE/tQWAI5BFtOFnQ3vTSOGq0jFX8Pe8HTEafY2tZFKHfwYU m7aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=Gian5uqoo2smFFIQzvd/1FzUwi87vdbk/rqem/OITc8=; b=qB1AUH6dLN/yYhO/vkgR6e/YXbajDENbA645MK5CFPePBHLCC8ryIDIu+6PFg8DOnr ++sGafLYvEkkSli0v0qmj1Rs1zfzql8+77Efhy6sAFE6lRrT+FXZqcd/jxSwRg+h0TB2 /gQVFOIV4bBHWigxLnsJWhuZEzqe4i2ssOd+z1h1jlzTePXwSTr+zk3y8Oeyup+HwxHD MwgD1a2+FmwZ/iVjrUKtRn3clvaSF0LW7HqaNg7onltGTVsSJY7d+yGwiLfn93Zvb8tM I6oQXdONybcSblVLuUHIuIlBWht8qqvEgp9Bd0BWoEeRRAERLmKw4osKWMSUFKt0scgX /KXg== X-Gm-Message-State: APjAAAUYyczU4/xCCdIWUxxpviwV9TKqGZMxoLaeY27CAPFd+jwlbOLP ZtiWapDc8joqlxpYaruF919DnDG3l9o= X-Google-Smtp-Source: APXvYqwu6Bair+BbVvavfiy4Yd0oVgAbnfHlfr+/TlO+NK99tpnMXlAryeNODyiVlcxwqzyQaHyyRQ== X-Received: by 2002:adf:ef8f:: with SMTP id d15mr1438737wro.67.1570001814948; Wed, 02 Oct 2019 00:36:54 -0700 (PDT) Received: from localhost.localdomain (217-149-168-9.nat.highway.telekom.at. [217.149.168.9]) by smtp.gmail.com with ESMTPSA id b62sm8850829wmc.13.2019.10.02.00.36.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 00:36:54 -0700 (PDT) Message-ID: <0503910ff326d635e681849565de20db63c63eca.camel@cybertec.at> Subject: Re: Users, Roles and Connection Pooling From: Laurenz Albe To: Matt Andrews , "pgsql-general@lists.postgresql.org" Date: Wed, 02 Oct 2019 09:36:53 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.4 (3.32.4-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 2019-10-02 at 08:46 +1000, Matt Andrews wrote: > Here’s a question I’ve been asking for a while and just can’t find an > answer to, so I thought I’d ask it here. The answer could be > subjective, but here goes... > > When a web app connects to Postgres via a connection pooler, what is > the best way to manage privileges for the connecting user? Especially > when their is a complex hierarchy of privileges? > > Should each signed up user have their own role which inherits from > whichever roles they are members of? This means that the connection > pool role should then be granted in every user? > > Does creating a Postgres role for every app user scale? Roles can > only have names, is an email address a good name for individual app > users? > > Are their any resources out there that anyone knows of that addresses > these questions? > > There’s plenty of stuff out there on roles and permissions, I’ve read > a lot of it, but not much on dealing with individual app users > through a connection pool. > > I’m thinking that any queries should start with SET ROLE ‘username’ > and end with RESET ROLE. Is this how it could work? > > Any help would be greatly appreciated. A couple of pointers: - The role that the application server connects with should be a member of each database role it wants to become through SET ROLE. - Yes, you will have to start with SET ROLE. RESET ROLE should be executed by the pool when it gets a connection back. - This is a good setup if you don't have too many users. Metadata queries will start getting slow if you get into the tens of thousands of users, maybe earlier. The advantages are that you can make use of PostgreSQL's rich permission concept and row level security. - The name of the user might as well be an e-mail address, as long as it does not exceed 63 bytes. - Make use of user groups and grant privileges on that level rather than to the individual users. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com