Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Created September 27, 2023 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbietunnie/eb7df40be7eb5d96f906a915854d0733 to your computer and use it in GitHub Desktop.
Save barbietunnie/eb7df40be7eb5d96f906a915854d0733 to your computer and use it in GitHub Desktop.
How to create a read-only PostgreSQL user

How to create a read-only PostgreSQL user

  1. Create a new user in PostgreSQL

    CREATE USER <username> WITH PASSWORD '<password>';
    
  2. GRANT CONNECT access

    GRANT CONNECT ON DATABASE <database_name> TO <username>;
    
  3. GRANT USAGE on schema

    GRANT USAGE ON SCHEMA <schema_name> TO <username>;
    
  4. GRANT SELECT access using any of the options below

    • Grant SELECT for a specific table:

      GRANT SELECT ON <table_name> TO <username>;
      
    • Grant SELECT for multiple tables

      GRANT SELECT ON ALL TABLES IN SCHEMA <schema_name> TO <username>;
      
    • To grant access to the new tables in the future automatically, the default needs to be altered

      ALTER DEFAULT PRIVILEGES IN SCHEMA <schema_name> GRANT SELECT ON TABLES TO <username>;
      
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment