Raja's Exocortex

User Creation for Postgres, MysSQL and MariaDB

Create read-write and read-only users for Postgres and MySQL/MariaDB

  1. _DATABASENAME
  2. _USERNAME -- user with full privileges
  3. _PASSW0RD
  4. _USERNAME_READER -- User with read-only privileges
  5. _PASSW0RD_READER
-- Postgres read-write user, has all privileges in this DB
CREATE USER _USERNAME WITH PASSWORD '_PASSW0RD';
CREATE DATABASE _DATABASENAME OWNER _USERNAME;
GRANT ALL PRIVILEGES ON DATABASE _DATABASENAME TO _USERNAME;
-- Postgres read-only user
CREATE USER _USERNAME_READER WITH PASSWORD '_PASSW0RD';
GRANT CONNECT ON DATABASE _DATABASENAME TO _USERNAME_READER;

\c _DATABASENAME
-- Add more schemas and tables as required
GRANT USAGE ON SCHEMA _DATABASENAME.public TO _USERNAME_READER;
-- Grant RO privilege on all existing tables
GRANT SELECT ON ALL TABLES IN schema _DATABASENAME.public TO _USERNAME_READER;

-- user will get RO privilege on all new tables automatically
ALTER DEFAULT PRIVILEGES IN SCHEMA _DATABASENAME.public GRANT SELECT ON TABLES TO _USERNAME_READER;
-- MySQL/MariaDB:
CREATE USER '_USERNAME_READER'@'10.200.2.3%' IDENTIFIED BY '_PASSW0RD_READER';
GRANT SELECT, SHOW VIEW ON _DATABASENAME.* TO '_USERNAME_READER'@'10.200.2.3%';
FLUSH PRIVILEGES;

#postgres #mariadb #mysql #user-creation