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 2015 October PostgreSQL: Script to Create a Read-Only Database User

PostgreSQL: Script to Create a Read-Only Database User

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

In this post, I am sharing the help on how to create, a read-only user with limited access in PostgreSQL Database Server?.

As a Database Administrator, you should create different database users for the different purposes.
In the standard application, there are a few various types of Database Users like Application user, Read-only user, Admin user.

You should always create a shared database role group and assign roles to the user.

Here, I am sharing one script to create the read only user with limited access to PostgreSQL Database. This user can only SELECT table data and also restricted by schema access.

Create the Read-Only user:

1
2
CREATE ROLE Read_Only_User WITH LOGIN PASSWORD 'Test1234'
NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION VALID UNTIL 'infinity';

Assign permissions to the read only user:

1
2
3
4
GRANT CONNECT ON DATABASE YourDatabaseName TO Read_Only_User;
GRANT USAGE ON SCHEMA public TO Read_Only_User;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO Read_Only_User;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO Read_Only_User;

In the above script, you can find that I restricted user by Database, Schema and gave only SELECT permission for tables.

If you need to give any additional permission like EXECUTE permission for functions, use below script.

1
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO Read_Only_User;

Oct 22, 2015Anvesh Patel
SQL Server: Script to find Last Backup Time for All DatabasesSQL Server: Script to find all Default values with Columns
Comments: 9
  1. Michael
    February 25, 2016 at 7:32 am

    Hi!
    I found Your website very useful and recently I got into this particular article.
    What I wanted to share is a little addition which I tried many times and which is about forbidding a user from making any transactions on the DB.
    Apart from what You’ve showed above, one could add

    alter user set default_transaction_read_only = on;

    This little piece of code is specially useful when creating a user account to handle backup.

    Once again – great job with this website!

    • Anvesh Patel
      Anvesh Patel
      February 27, 2016 at 7:47 am

      Thanks, Michael for your input.
      We can also add this.

    • Ana
      May 16, 2016 at 9:53 pm

      Hi! The information provided is very helpful.

      I am having trouble forbidding not a user but a group of users from making transaction on DB. Any idea of how to that using what Michael is proposing?

      Thanks in advance

      • Anvesh Patel
        Anvesh Patel
        May 17, 2016 at 4:43 am

        Yes, you can set default_transaction_read_only to a particular user or group of the user.
        This parameter controls the default read-only status of each new transaction.
        The default is off.

  2. Pippo
    March 27, 2018 at 4:37 am

    How do you prevent readonly user to create new tables?

    • Anvesh Patel
      Anvesh Patel
      March 28, 2018 at 7:24 pm

      REVOKE CREATE ON SCHEMA public FROM public;

  3. anupam vishwakarma
    May 30, 2018 at 12:13 pm

    Hi Anvesh,
    I followed all the steps but there is error wile accessing tables . please suggest
    psql -d postgres -Umonitor_123 -W
    Password for user monitor_rplus:
    psql (9.2.13, server 9.2.2)
    Type “help” for help.

    postgres=> \l
    List of databases
    Name | Owner | Encoding | Collate | Ctype | Access privileges
    ————-+———-+———-+———+——-+————————–
    postgres | postgres | LATIN1 | C | C |
    database1 | postgres | LATIN1 | C | C | =Tc/postgres +
    | | | | | postgres=CTc/postgres +
    | | | | | rplus=CTc/postgres +
    | | | | | monitor_rplus=c/postgres
    template0 | postgres | LATIN1 | C | C | =c/postgres +
    | | | | | postgres=CTc/postgres
    template1 | postgres | LATIN1 | C | C | =c/postgres +
    | | | | | postgres=CTc/postgres
    (4 rows)

    postgres=> \dt
    No relations found.
    postgres=> \c database1
    Password for user monitor_123:
    psql (9.2.13, server 9.2.2)
    You are now connected to database “revenueplus” as user “monitor_rplus”.
    revenueplus=> \dt
    List of relations
    Schema | Name | Type | Owner
    ——–+————————————+——-+——-
    public | foo | table | xxxxx

    database1=> select count(*) from foo;
    ERROR: permission denied for relation foo

    • Anvesh Patel
      Anvesh Patel
      May 30, 2018 at 7:22 pm

      Did you give the schema permission? Is foo by default created in public schema?

  4. Abhi
    January 26, 2019 at 2:24 pm

    Can we grant a user access to different databases in postgres?
    If so, how to grant read only access to different databases. Thanks in advance

Anvesh Patel
Anvesh Patel

Database Engineer

October 22, 2015 PostgreSQL, PostgreSQL DBA ScriptAnvesh Patel, database, database research and development, dbrnd, permission, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, PostgreSQL Tuning, role, security, user
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....