CLOUD & DATABASES
PostgreSQL user generator
Generate SQL to create a PostgreSQL login role and either grant it selected privileges on a database schema or make it the owner of the database.
CHANGE_ME is a placeholder. Replace it here with a strong password. PostgreSQL stores it as a hash (SCRAM-SHA-256 by default).
SQL output
-- Create the login role if it does not exist (an existing password is not changed) DO $do$ BEGIN IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'app_user') THEN CREATE ROLE app_user WITH LOGIN PASSWORD 'CHANGE_ME'; END IF; END $do$; -- Allow connecting to the database GRANT CONNECT ON DATABASE app_db TO app_user; -- Switch to the database (psql) – the following statements only apply to the current database \connect app_db -- Allow access to the schema GRANT USAGE ON SCHEMA public TO app_user; -- Grant the selected privileges on existing tables and sequences GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_user; -- Grant them on tables and sequences created later by the role running this script ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO app_user;
Run the script as a superuser with psql, e.g. psql -U postgres -f users.sql. \connect is a psql command; in other clients, run the statements below it in a connection to the target database.
The output contains the password in plain text. Keep it confidential and execute it only over a trusted connection.
SQL is generated in your browser and is never transmitted.
Frequently asked questions
What is the difference between a user and a role in PostgreSQL?
PostgreSQL only knows roles. A user is a role with the LOGIN attribute – CREATE USER is just an alias for CREATE ROLE … LOGIN. Unlike MySQL, a role has no host part: which hosts a role may connect from and with which authentication method is configured in pg_hba.conf.
Should the application role own the database or only get privileges?
Ownership is the simplest option when one application uses its own database and runs its migrations itself: the owner can create and change everything. Selected privileges are the better choice for additional roles, e.g. a read-only role for reporting, or when migrations run under a separate role.
Why does the script contain \connect?
Privileges on schemas, tables and sequences belong to a single database and can only be granted while connected to it. \connect is a psql command that switches the connection. In pgAdmin or DBeaver, open a connection to the target database instead and run the remaining statements there. CREATE DATABASE cannot run inside a transaction block, so run the script with autocommit.
Why do new tables not get the privileges automatically?
GRANT … ON ALL TABLES only affects tables that already exist. For tables created later, ALTER DEFAULT PRIVILEGES is needed – it applies to objects created by the role that runs the statement. If another role (e.g. a migration role) creates the tables, run it as that role or add FOR ROLE <name>. Sequences need their own privileges so that serial and identity columns work.
How do I avoid the password in plain text?
The SQL is generated entirely in your browser and is never transmitted. On the server, however, the statement may end up in the log (e.g. with log_statement = all) or in the psql history. Alternatively, create the role without a password and then set it in psql with \password <role>: psql hashes the password locally and only sends the hash.