User Creation for Postgres, MysSQL and MariaDB
Create read-write and read-only users for Postgres and MySQL/MariaDB
- _DATABASENAME
- _USERNAME -- user with full privileges
- _PASSW0RD
- _USERNAME_READER -- User with read-only privileges
- _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