How to Grant Permissions in PostgreSQL

Galaxy Glossary

How do I grant permissions in PostgreSQL?

GRANT assigns specific privileges on database objects to roles, controlling who can read, write, or administer them.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

How to Grant Permissions in PostgreSQL

Use the GRANT statement to give users or roles precise privileges on databases, schemas, tables, sequences, functions, and more. It is the cornerstone of fine-grained access control.

What problem does GRANT solve?

GRANT lets you avoid one superuser for everything. You can hand out only the rights a developer, analyst, or app needs, keeping data safe and audits simple.

What is the basic GRANT syntax?

The format is GRANT privilege_list ON object_type object_name TO role_list [WITH GRANT OPTION]. Replace each placeholder with your objects and roles.

How do I grant read-only access to one table?

Run GRANT SELECT ON public.sales TO analyst;. The analyst role can now query public.sales but cannot modify it.

How can I let a role create tables in a schema?

First make the schema owner execute GRANT CREATE ON SCHEMA reporting TO loader;. Then log in as loader and create tables freely inside reporting.

How do I grant permissions on all current and future tables?

Combine a direct GRANT with ALTER DEFAULT PRIVILEGES:GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;

Can I delegate privileges to others?

Add WITH GRANT OPTION. Example: GRANT SELECT ON customers TO team_lead WITH GRANT OPTION;. The team_lead may now pass SELECT to colleagues.

Best practices for GRANT

Grant to roles, not users

Create roles like reader, writer, admin. Then GRANT reader TO alice;. This keeps permissions tidy when staff changes.

Revoke before grant for least privilege

Start with REVOKE ALL if you inherit an environment with broad rights. Then grant back only what is necessary.

How to list current privileges?

Run \dp tablename in psql. It shows who has which rights on the object.

When should I use GRANT … ON DATABASE?

Grant database-level CONNECT and TEMP rights to permit session creation and temp table usage. Everything else should be on schema or table level.

Why How to Grant Permissions in PostgreSQL is important

How to Grant Permissions in PostgreSQL Example Usage


-- Give app_user power to read and write specific tables
GRANT SELECT, INSERT, UPDATE
ON orders, customers
TO app_user;

How to Grant Permissions in PostgreSQL Syntax


GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER }
     [, ...] | ALL [ PRIVILEGES ] }
ON [ TABLE ] table_name [, ...]
TO { role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ];

GRANT { CREATE | CONNECT | TEMP | ALL [ PRIVILEGES ] }
ON DATABASE database_name
TO role_name [, ...] [ WITH GRANT OPTION ];

GRANT { USAGE | CREATE | ALL [ PRIVILEGES ] }
ON SCHEMA schema_name
TO role_name [, ...] [ WITH GRANT OPTION ];

GRANT { EXECUTE | ALL [ PRIVILEGES ] }
ON FUNCTION function_name(arg_types) [, ...]
TO role_name [, ...] [ WITH GRANT OPTION ];

Common Mistakes

Frequently Asked Questions (FAQs)

Can I grant all privileges in one command?

Yes. Use GRANT ALL PRIVILEGES ON table_name TO role;. It maps to every valid object-level right.

Does GRANT overwrite existing privileges?

No. GRANT adds privileges. Use REVOKE or ALTER DEFAULT PRIVILEGES to remove or change existing ones.

How do I undo a mistaken GRANT?

Run REVOKE privilege_list ON object FROM role;. The change takes effect immediately for new queries.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo