How to create a user, database, and assign the user to the db in PostgreSQL

Here’s a quick reference on how to create a user, a database and grant the user all privileges to the database (on Mac OS X). If you haven’t yet installed PostgreSQL, look here: How to install PostgreSQL on Mac OS X

Create user

In terminal, enter:

createuser -P user_name

And follow the instructions – they’ll probably be:

  enter password for new role:
  Enter it again:
  Shall the new role be a superuser? (y/n) n
  Shall the new role be allowed to create databases? (y/n) n
  Shall the new role be allowed to create more new roles? (y/n) n

Create database

In terminal, enter:

createdb db_name

Grant privileges to user

In terminal:

psql db_name

Then:

grant all privileges on database db_name to user_name;

That’s it!