PostgreSQL: Create Database, User and Grant Privileges


Open terminal and invoke this command:

psql -U postgres

to create new database, invoke this command:

CREATE DATABASE mydatabase;

to create new user, invoke this command:

CREATE USER dbuser WITH PASSWORD 'mypassword';

then, you can set your desired user-related preference like this

ALTER ROLE dbuser SET client_encoding TO 'utf8';
ALTER ROLE dbuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE dbuser SET timezone TO 'UTC';

to grant privileges, invoke this command:

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO dbuser;

Quit from PostgreSQL console

\q