TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Row Level Security with PostgreSQL 9.5

129 pointsby thomcroweabout 9 years ago

8 comments

karavelovabout 9 years ago
I think the article is wrong on assiming and calling out that RLS is only useful if you use only the DB roles for security mechanism.<p>When you create a RLS policy you specify a predicate (the USING and WITH CHECK parts) that is checked for each accessed row (read or write). The predicate is not in any way restricted to refer to a DB role it can compare for example a field with a parameter variable.<p>EDIT: Here is a gist how to not use it without roles for the permissions: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;luben&#x2F;4ab60b0dbda66ecf4b6601b88c852272" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;luben&#x2F;4ab60b0dbda66ecf4b6601b88c8522...</a>
评论 #11537412 未加载
elchiefabout 9 years ago
You can use RLS with a web app.<p>You can still use connection pooling.<p>PostgreSQL is performant with hundreds of thousands of user accounts (though not pgAdmin).<p><pre><code> create role www noinherit login password &#x27;s3cr3t&#x27;; create role alice; grant alice to www; </code></pre> Connect to database as www (same connection string = you can use pooling)<p>After you get your connection from the pool<p><pre><code> set role alice; </code></pre> When you release you connection back to the pool<p><pre><code> reset role; </code></pre> Want per-database users instead of per-cluster users? See the db_user_namespace setting<p>Use LDAP &#x2F; AD? Sync users&#x2F;groups&#x2F;roles to pg: <a href="https:&#x2F;&#x2F;github.com&#x2F;larskanis&#x2F;pg-ldap-sync" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;larskanis&#x2F;pg-ldap-sync</a>
评论 #11539524 未加载
评论 #11538241 未加载
评论 #11539066 未加载
orfabout 9 years ago
So why does this feature require real database users? Why couldn&#x27;t a statement be added like &quot;SET current_user = some_id;&quot; that is called once per connection and have the row level security authenticate against that? Sure, it&#x27;s not real security, but it&#x27;s a step up from a single user being able to select everything in your database through a single SQLi as long as you ensure that the &quot;current_user&quot; is the first statement executed.
评论 #11537116 未加载
评论 #11537290 未加载
joshmaabout 9 years ago
Has anyone used RLS in a standard web service? Are there any common use cases where this is handy? I&#x27;m thinking it won&#x27;t work for your standard permissioning system since it seems clunky if you have any slightly sophisticated logic (X is in org Y and can see anything Y has read access to).<p>Maybe it&#x27;d be more useful if you&#x27;re using a single DB for a multi-tenant setup, and you know each tenant&#x27;s data is strictly isolated?
评论 #11536623 未加载
Jweb_Guruabout 9 years ago
Row-level security can be respected by pg_dump as well, which means it can be used (in a multitenant context) to do things like migrate just a single tenant between databases.
评论 #11537087 未加载
davidwabout 9 years ago
&gt; But, you do have to enable it for each table plus you need to commit to using database roles as a main security mechanism. That last part is the barrier but also the reason to use such a feature.<p>That&#x27;s kind of a sticking point for web apps. Wonder if there&#x27;s a way around that.
评论 #11536485 未加载
评论 #11536442 未加载
hergeabout 9 years ago
This is completely not pertinent to the actual content of the article, but I think there a slight error in the first paragraph of the article. Jsonb support was added as part of Postgres 9.4, at least according to <a href="http:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;9.4&#x2F;static&#x2F;release-9-4.html" rel="nofollow">http:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;9.4&#x2F;static&#x2F;release-9-4.html</a>, right?<p>I ask because we&#x27;re planning on using jsonb here soon, and we were hoping it would be available in the version of Postgres available in ubuntu 16.04.
评论 #11536206 未加载
评论 #11536531 未加载
awinter-pyabout 9 years ago
Relying on the PG user&#x2F;role system doesn&#x27;t feel general enough to me.<p>A better solution would set a query on the connection that constrains which rows it can touch. I know this ends up being more complicated and less performant but it splits the difference between app-level and database-enforced security.