SQL GRANT statement generator
Pick an engine, a principal and the privileges. You get the exact GRANT for SQL
Server, PostgreSQL, MySQL or Oracle, the REVOKE that undoes it, and the steps each
engine quietly requires: schema USAGE, default privileges, role membership,
CREATE SESSION. It runs in your browser, and nothing you type leaves your device.
Generated statements
Example: read-only access to one PostgreSQL schema
GRANT USAGE ON SCHEMA public TO app_readonly;
Without USAGE on the schema, none of the privileges below can be reached.
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_readonly;
Covers every table and view that exists today.
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO app_readonly;
Extends the same grant to tables created later, per role that creates them.
How permissions work in each engine
The four engines use the same verbs for very different permission models. Knowing what a "user", a "role" and a "schema" mean in each one explains most of the statements above.
SQL Server: logins, users and schemas
A login signs in to the server; a user is the login's identity inside one database, created
with CREATE USER ... FOR LOGIN. Permissions attach to users and roles, and to
securables such as OBJECT:: (a single table or procedure) and SCHEMA::
(everything in a schema, now and later). Roles are database-local: grant the privileges to the
role once, then ALTER ROLE ... ADD MEMBER each user. There is no USAGE
privilege. Two surprises: NEXT VALUE FOR нужно UPDATE on the sequence,
and revoking a permission that was granted WITH GRANT OPTION fails unless you add
CASCADE.
PostgreSQL: everything is a role
A user is just a role with the LOGIN attribute, and any role can be granted to
another, so a "read-only role" is a regular role that users join and inherit from. Privileges
are per-database: nothing in a GRANT names a database, so run the statements in the
right one. Two PostgreSQL steps have no equivalent elsewhere: USAGE ON SCHEMA,
without which table privileges cannot be reached at all, and ALTER DEFAULT
PRIVILEGES, without which tables created later stay closed. Default privileges only
apply to objects created by the role that ran the statement (or the one named in
FOR ROLE). Functions are the reverse case: every new function is executable by
PUBLIC until you revoke that.
MySQL: accounts are 'user'@'host'
An account is a name plus the host it connects from, written 'app_user'@'%'. MySQL
collapses database and schema into one word, so db.* covers every table, view and
routine in it, including future ones, which is why there is no default-privileges step.
USAGE means the opposite of PostgreSQL: no privileges at all. Since MySQL 8 a
GRANT no longer creates a missing account, so create the user explicitly first.
Granted roles are inactive until SET DEFAULT ROLE или SET ROLE turns
them on. MariaDB differs here: its roles have no host part, and the default is set with
SET DEFAULT ROLE role FOR user, so adjust the role recipe before running it there.
Oracle: users own schemas
Every user owns a schema of the same name, and the objects you grant on are usually another
user's. Oracle 23ai added schema privileges: GRANT SELECT ANY TABLE ON SCHEMA sales TO
app_user covers every table and view in sales, including ones created later.
On 19c and earlier grants are per-object (ON SALES.ORDERS), so a whole schema
takes a PL/SQL loop that you rerun when tables are added. New accounts cannot log in until they
hold CREATE SESSION. Rows inserted into another user's table count against the
owner's tablespace quota, not the inserter's, so a user that only writes to other schemas needs
no ALTER USER ... QUOTA.
Privilege reference
What each privilege allows, and where the engines disagree about it.
| Privilege | What it allows | Различия между движками |
|---|---|---|
SELECT | Read rows, and read a sequence's current value. | On an Oracle sequence, SELECT is what permits NEXTVAL and CURRVAL. On a PostgreSQL sequence it permits currval() only. |
INSERT | Add rows. | PostgreSQL inserts into serial columns also need USAGE on the sequence; identity columns do not. SQL Server accepts no column list on INSERT. |
UPDATE | Change rows. | Every engine accepts a column list: UPDATE (price). On a SQL Server sequence, UPDATE is what permits NEXT VALUE FOR. |
DELETE | Remove rows. | Does not cover TRUNCATE. PostgreSQL has a TRUNCATE privilege; SQL Server needs ALTER on the table, MySQL needs DROP, and Oracle needs DROP ANY TABLE unless you own the table. |
EXECUTE | Run a function or procedure. | PostgreSQL grants EXECUTE to PUBLIC by default on new functions; revoke it, or a narrow grant restricts nothing. SQL Server table-valued functions need SELECT, not EXECUTE. |
USAGE | Use a containing object: a schema or a sequence. | PostgreSQL: required for schema access and sequence values. MySQL: GRANT USAGE means no privileges. SQL Server and Oracle: does not exist. |
WITH GRANT OPTION | Let the principal grant the privilege on to others. | Supported by all four, but Oracle refuses it on an object grant to a role. SQL Server and PostgreSQL need REVOKE ... CASCADE to take a grantable permission back once it was passed on, and a SQL Server role member passes it on with GRANT ... AS role. MySQL revokes the option with its own REVOKE GRANT OPTION statement. Oracle 23ai schema privileges use WITH ADMIN OPTION instead. |
Read-only and read-write role recipes
Grant once to a role, then add and remove users from it. Tick "Add a read-only / read-write role recipe" in the generator to get these for your own database, schema and user.
SQL Server
CREATE ROLE [sales_readonly]; GRANT SELECT ON SCHEMA::[dbo] TO [sales_readonly]; ALTER ROLE [sales_readonly] ADD MEMBER [app_user]; CREATE ROLE [sales_readwrite]; GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::[dbo] TO [sales_readwrite]; ALTER ROLE [sales_readwrite] ADD MEMBER [app_user];
PostgreSQL
CREATE ROLE sales_readonly NOLOGIN; GRANT USAGE ON SCHEMA sales TO sales_readonly; GRANT SELECT ON ALL TABLES IN SCHEMA sales TO sales_readonly; ALTER DEFAULT PRIVILEGES IN SCHEMA sales GRANT SELECT ON TABLES TO sales_readonly; GRANT sales_readonly TO app_user; CREATE ROLE sales_readwrite NOLOGIN; GRANT sales_readonly TO sales_readwrite; GRANT INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA sales TO sales_readwrite; ALTER DEFAULT PRIVILEGES IN SCHEMA sales GRANT INSERT, UPDATE, DELETE ON TABLES TO sales_readwrite; GRANT USAGE ON ALL SEQUENCES IN SCHEMA sales TO sales_readwrite; ALTER DEFAULT PRIVILEGES IN SCHEMA sales GRANT USAGE ON SEQUENCES TO sales_readwrite; GRANT sales_readwrite TO app_user;
MySQL
CREATE ROLE 'sales_readonly'; GRANT SELECT ON `sales`.* TO 'sales_readonly'; GRANT 'sales_readonly' TO 'app_user'@'%'; CREATE ROLE 'sales_readwrite'; GRANT SELECT, INSERT, UPDATE, DELETE ON `sales`.* TO 'sales_readwrite'; GRANT 'sales_readwrite' TO 'app_user'@'%'; SET DEFAULT ROLE ALL TO 'app_user'@'%';
Oracle (23ai)
CREATE ROLE sales_readonly; GRANT CREATE SESSION TO sales_readonly; GRANT SELECT ANY TABLE ON SCHEMA sales TO sales_readonly; GRANT sales_readonly TO app_user; CREATE ROLE sales_readwrite; GRANT sales_readonly TO sales_readwrite; GRANT INSERT ANY TABLE, UPDATE ANY TABLE, DELETE ANY TABLE ON SCHEMA sales TO sales_readwrite; GRANT sales_readwrite TO app_user;
On Oracle 19c and earlier there is no ON SCHEMA grant. Grant to the role table by
table instead, and run the loop again after new tables are created:
BEGIN
FOR t IN (SELECT table_name FROM all_tables WHERE owner = 'SALES') LOOP
EXECUTE IMMEDIATE 'GRANT SELECT ON SALES."' || t.table_name || '" TO SALES_READONLY';
END LOOP;
END;
/
GRANT statement FAQ
Is this GRANT generator free?
Yes. There is no sign-up and no usage limit. SQLly publishes it alongside its other free tools.
Is anything I type sent to a server?
No. The generator is a small script that runs in your browser tab. Nothing you type is uploaded, logged or stored, and once the page has loaded it keeps working offline. The "Copy link to this setup" button puts your choices after the # in the address, and browsers never send that part to a server.
Why do the PostgreSQL statements not name a database?
A PostgreSQL GRANT on a table or schema applies inside the database you are connected to, and there is no syntax for naming another one. Connect to the right database first and run the statements there. The generator adds a USE statement for SQL Server and writes the database into the MySQL grant, because those two can address it.
What is the difference between a user and a role?
All four engines separate the thing that logs in from the bundle of permissions. A role is a named set of privileges you grant once and hand to many users. PostgreSQL goes furthest: every user is a role with the LOGIN attribute, and members inherit a role's privileges. The recipes section shows the read-only and read-write role setup for each engine.
How do I grant access to tables created in the future?
SQL Server and MySQL make this automatic: a grant on a schema or on a database covers objects added later. PostgreSQL needs ALTER DEFAULT PRIVILEGES, which only applies to objects created by the role that ran it, or the role named in FOR ROLE. Oracle 23ai has schema privileges such as GRANT SELECT ANY TABLE ON SCHEMA sales TO app_user, which cover future tables. On Oracle 19c and earlier, new tables need the per-table grant run again.
What does USAGE mean?
It is the permission to use a containing object, not to read data. In PostgreSQL a principal without USAGE on a schema cannot reach the tables inside it, and an INSERT that fills a serial column needs USAGE on its sequence (identity columns do not). MySQL reuses the word differently: GRANT USAGE there means no privileges at all and is used to create an account or alter it without granting anything.
How do I take the permissions away again?
Pick "GRANT, plus the matching REVOKE" and every grant comes with the statement that undoes it, including taking members out of a role. Three things to know: a user keeps access through a role it is a member of even after a direct revoke; SQL Server refuses to revoke a permission granted WITH GRANT OPTION unless you add CASCADE, and PostgreSQL refuses once the principal has passed it on; and in MySQL the grant option survives a REVOKE of the privileges and needs its own REVOKE GRANT OPTION ON ... FROM ... statement.
Does the generator handle column-level privileges?
Not yet. Generate the table-level statement and add the column list in parentheses after the privilege name, for example GRANT SELECT (email, created_at) ON dbo.customers TO app_user. Support differs: PostgreSQL and MySQL accept a column list on SELECT, INSERT and UPDATE; SQL Server on SELECT and UPDATE; Oracle on INSERT and UPDATE only.
Like the generator? Meet the rest of SQLly
A native SQL workbench for macOS, Windows and Linux with IntelliSense that knows your schema, guardrails for risky queries, and a formatter built into the editor.