Create Db/User/Schema: Create Database PSQL / Createdb Utility
Create Db/User/Schema: Create Database PSQL / Createdb Utility
Grant:
• Grant CONNECT to the database:
GRANT CONNECT ON DATABASE database_name TO username;
• Grant USAGE on schema:
GRANT USAGE ON SCHEMA schema_name TO username;
• Grant on all tables for DML statements: SELECT, INSERT, UPDATE,
DELETE
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA
schema_name TO username;
• Grant all privileges on all tables in the schema:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO
username;
• Grant all privileges on all sequences in the schema:
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA schema_name
TO username;
• Grant permission to create database:
ALTER USER username CREATEDB;
• Make a user superuser:
ALTER USER myuser WITH SUPERUSER;
• Remove superuser status:
ALTER USER username WITH NOSUPERUSER;
• Column Level access:
GRANT SELECT (col1), UPDATE (col1) ON mytable TO user;
Revoke Examples
• Show search path can be used to find the current search path.
Example:
postgres=# show search_path;
search_path
-----------------
"$user", public
( 1 row)
• Default "$user" is a special option that says if there is a
schema that matches the current user (i.e SELECT SESSION_USER;),
then search within that schema.
• Search path can be set at session level, user level, database
level and cluster level
Example:
Test1=# SET search_path TO test1,public;
Test1=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
test1 | abc | table | test1
(1 rows)