Create a new database and user in Postgres

Create a new database and user in Postgres
Photo by Trey Musk / Unsplash

At REPLIQ we tend to use open source solutions for most of our projects. The right tools for the right job. Postgres is a very powerful database that powers services and businesses across the world.

Postgres also works well with Django projects. This is a quick way to get started with a freshly installed Postgres instance.

1. sudo -u postgres psql
2. postgres=# CREATE DATABASE django;
3. postgres=# CREATE USER django WITH ENCRYPTED PASSWORD 'supersecret';
4. postgres=# GRANT ALL PRIVILEGES ON DATABASE django TO django;
4. postgres=# ALTER DATABASE django OWNER TO django;

Line 1

Change to the postgres user and run the psql command to get into the Postgres shell.

Line 2

postgres=# CREATE DATABASE django; creates the database.

Line 3

postgres=# CREATE USER django WITH ENCRYPTED PASSWORD 'supersecret'; creates a new user and sets the password for the new user.

Line 4

postgres=# GRANT ALL PRIVILEGES ON DATABASE django TO django; Gives the new user access to the newly created database.

Pay attention to the grant all privileges part since it will give full control of the database to the new user. You can read more about privileges in Postgres here