This assumes the new user is kierank and the database is hackatime
To generate a strong 16-character password, you can use the following command:
openssl rand -base64 16This will produce a random password. For example:
q9Km7Txp4zVRh2Rg
Log in to your PostgreSQL instance using a superuser (e.g., postgres) account:
su postgres
psqlExecute the following replacing kierank and pass with your user and password generated above
CREATE USER kierank WITH PASSWORD 'pass';Grant the user kierank access to connect to the hackatime database:
GRANT CONNECT ON DATABASE hackatime TO kierank;Switch to the hackatime database:
\c hackatimeGrant the user kierank usage rights on the schema (usually public unless a different schema is used):
GRANT USAGE ON SCHEMA public TO kierank;Grant kierank permissions for all existing tables in the public schema:
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO kierank;If sequences are used for auto-increment fields (e.g., SERIAL), grant kierank the necessary permissions:
GRANT USAGE, SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA public TO kierank;To ensure kierank automatically gets the appropriate permissions on any newly created tables or sequences in the future, run these commands:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO kierank;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT, UPDATE ON SEQUENCES TO kierank;To verify the permissions, you can use the following query to list the privileges granted to kierank:
\dpThis will display the access privileges for all tables, sequences, and views in the public schema.
If you need to update or revoke permissions in the future, you can use commands like:
-
Revoke specific permissions:
REVOKE SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public FROM kierank;
-
Grant additional permissions:
GRANT TRUNCATE ON ALL TABLES IN SCHEMA public TO kierank;
-
Tables (
arwd):a: INSERTr: SELECTw: UPDATEd: DELETE
-
Sequences (
rwU):r: USAGE (view current value)w: UPDATE (modify value)U: USAGE (set the value)