PostgreSQL (often just called Postgres) is a widely used open-source SQL database. Unlike SQLite, which is the default database that comes along with a new Django install, Postgres has to be installed and run in each environment.

Install and Run Postgres Locally

Install postgres in your dev environment (on your computer):

brew install postgresql@15

Postgres has to be running so that our application can connect to it. Homebrew gives us a command to manage background services, so to start Postgres we can run:

brew services start postgresql@15

This will start Postgres and tell your computer to start it again if you restart your computer. You can stop it with brew services stop postgresql or restart it with brew services restart postgresql if you ever need to do that.

Create a Postgres Database Locally

You will create a new Postgres database for each new Django application you build. The database is independent of your code, but we will want to connect to it using Django so that we can store our data.

When you install Postgres, you get a few command line utilities that are used with Postgres. We'll use them in the next few steps.

1. Create a database user

Postgres has the concept of database users with different levels of permissions, so for each database you need to create a user that will be used to connect to it. Different teams have different conventions for how to name your user and your database, but here's how we do it at Momentum.

We need to create the user before we can create the database. Create a new user with the following command, where <username> is the name of your user. You should use the same name for the user and for the database, and they should follow the name of the application. Both are needed to connect to the database in Django, and it's easier to remember and get right when they are both the same.

createuser -d <username>

If your application was called booknook you could have run createuser -d booknook. The -d option means that this user will be allowed to create new databases.