I keep forgetting how to do initial local setup of postgres for django projects, so here are the steps I generally follow:
- Install postgres locally (for mac I like postgres.app). If you get a return value from
which psql
, you should be good to go - Set up your local database:
$ psql
postgres=# CREATE USER myproject_user WITH PASSWORD 'myproject_user';
CREATE ROLE
postgres=# CREATE DATABASE myproject_main;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE myproject_main to myproject_user;
GRANT
postgres=# \q
The same credentials should go in your settings.py:
if IS_PRODUCTION:
# Note: this is for a heroku deployment
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject_main',
'USER': 'myproject_user',
'PASSWORD': 'myproject_user',
'HOST': 'localhost',
'PORT': '',
}
}
Now get your database set up via django:
python manage.py migrate
And you should be good to go! 🍾