Skip to content

Konnection101

IanSeng edited this page Mar 5, 2021 · 3 revisions

Helpful Backend Commands

  • To create an admin user: python3 manage.py createsuperuser.

  • Saving new python dependencies: pip freeze > requirements.txt.

Installing DB

Make sure you have install the required Python packages first above. The following commands are to be run within a virtual env.

Note: This is done on OSX

brew install postgresql

// To start PostgreSQL
brew services start postgresql

// To stop PostgreSQL
brew services stop postgresql

// To use the CLI and create the databse
createuser -s myprojectuser 
psql postgres
CREATE DATABASE myproject;
ALTER ROLE myprojectuser SET client_encoding TO 'utf8'; etc.

// Recreate the database
psql postgres
DROP DATABASE myproject; // make sure to INCLUDE THE SEMICOLON
CREATE DATABASE myproject;
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';

// Don't forget to do the following steps after recreating the db
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser


// Drop one particular table in your database
python manage.py dbshell // access local database
\dt                      // see existing tables
DROP TABLE <table_name>;
\q to quit

// This will work if you need to recreate the table
python manage.py dbshell 
DELETE FROM django_migrations WHERE app = 'app_name'; # e.g. posts
\q to quit
python manage.py makemigrations app_name
python manage.py migrate

If you're making any DB changes, make sure PostgreSQL has started then

python3 manage.py makemigrations
python3 manage.py migrate