Skip to content

Instantly share code, notes, and snippets.

@noahsark769
Created March 29, 2019 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahsark769/51783cc22adfa57a14e8dc77649fdc47 to your computer and use it in GitHub Desktop.
Save noahsark769/51783cc22adfa57a14e8dc77649fdc47 to your computer and use it in GitHub Desktop.
Local PSQL configuration for Django projects

I keep forgetting how to do initial local setup of postgres for django projects, so here are the steps I generally follow:

  1. Install postgres locally (for mac I like postgres.app). If you get a return value from which psql, you should be good to go
  2. 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! 🍾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment