Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 August PostgreSQL 9.5: Row Level Security by Example

PostgreSQL 9.5: Row Level Security by Example

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Hello guys, Since last two months I have published about the SQL Server Interview Questions and Answers.
During this time, I got lots of messages and requests about the PostgreSQL.

In this August – 2016, I am going to post about only PostgreSQL and will try to complete all pending requests of readers.

In this post, I am going to publish about The Row Level Security which is now a common feature of all new version of RDBMSs like SQL Server 2016, PostgreSQL 9.5.

Postgre SQL Row Level Security

With the concept of Row Level Security, we can restrict the user for a particular set of records.
Previously, we are doing this kind of restriction using by creating filtered VIEWS and assign related VIEWS to related Users.

PostgreSQL 9.5 has this Row Level Security feature which extends the overall security policy of the PostgreSQL.

Let me demonstrate this.

Execute below all steps using Postgres Super user:

First create two sample users:

1
2
CREATE USER abc LOGIN PASSWORD '123';
CREATE USER xyz LOGIN PASSWORD '321';
Create a table with sample data:
1
2
3
4
5
6
7
8
CREATE TABLE tbl_Employees
(
EmpID INT
,EmpName CHARACTER VARYING
);
INSERT INTO tbl_Employees VALUES
(1,'Anvesh'),(2,'Martin'),(3,'Roy')
,(4,'Jeeny'),(5,'Kavita'),(6,'Neevan');
Add one new column for row level security:
1
ALTER TABLE tbl_Employees ADD pgUser CHARACTER VARYING;
Update table with specific user:
1
2
UPDATE tbl_Employees SET pgUser = 'abc' WHERE EmpID IN (1,2,3);
UPDATE tbl_Employees SET pgUser = 'xyz' WHERE EmpID IN (4,5,6);
Give some DML grants to these users:
1
2
GRANT SELECT,UPDATE,INSERT,DELETE ON tbl_Employees TO abc;
GRANT SELECT,UPDATE,INSERT,DELETE ON tbl_Employees TO xyz;
Create a security policy on tbl_Employee for current logged-in user only:
1
2
CREATE POLICY policy_employee_user ON tbl_Employees FOR ALL
TO PUBLIC USING (pgUser = current_user);
Enable ROW LEVEL SECURITY on tbl_Employees:
1
ALTER TABLE tbl_Employees ENABLE ROW LEVEL SECURITY;
Execute this SELECT with the different logins:
1
SELECT *FROM tbl_Employees;
Result of User abc:
1
2
3
4
5
empid empname pguser
--------------------------
1 Anvesh abc
2 Martin abc
3 Roy abc
Result of User xyz:
1
2
3
4
5
empid empname pguser
--------------------------
4 Jeeny xyz
5 Kavita xyz
6 Neevan xyz
Aug 1, 2016Anvesh Patel
MySQL: How to escape apostrophe and single quote?PostgreSQL: The Schema Search Path and change the default PUBLIC Schema
Anvesh Patel

Database Engineer

August 1, 2016 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, plpgsql, Postgres Query, postgresql, postgresql 9.5, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, row level security, security
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....