Skip to content

Setting up Backend Server

Grab all the tools:

Before spinning up the backend server we would need the following tools to be installed so that we can create a development environment for The Dev starter Backend template to spin up.

1. PostgreSQL

MacOs

  1. Install Homebrew official docs
Terminal window
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Install Postgres
Terminal window
brew install postgresql@14
  1. Start Postgresql
Terminal window
brew services start postgresql@14
  1. Create a database for your project
Terminal window
psql -d postgres
create database <project name>;
create user <user name> with password '<password>';
grant all on database <project name> to <username>

PermissionDenied for Creating Schema

if you’re facing errors like

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (permission denied for schema public

then select the db you created above and run the follwoing commands in the database server.

GRANT ALL ON SCHEMA public TO <username>;
GRANT ALL ON ALL TABLES IN SCHEMA public to <username>;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public to <username>;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to <username>;

Once we have the database created we need to set the env variables corresponding to <user name> , <project name> and <password> in the backend env, we can create the virtual environment for the backend project using poetry

2. Poetry

The DevStarter Backend boilerplate uses poetry to create and manage the dependencies since it helps with maintaining the dependencies using a lock file and helps in long run to keep the dependencies clean and running with right versions. official Installation docs

to install poetry on (Linux, macOS, Windows (WSL)) ,

Terminal window
curl -sSL https://install.python-poetry.org | python3 -

Windows (Powershell)

Terminal window
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -

once poetry is installed we can create a virtual environment with the following commands

Terminal window
#inside the backend project root create a virtual environment and activate it
poetry shell
#install the dependencies
poetry install

once the dependencies have been installed we can start the development server using:

python manage.py runserver

once the server is starter we can explore the swagger docs for the API at http://localhost:8000/api/docs now to access the API and admin we need to create database migrations and a superuser:

python manage.py makemigrations
python manage.py migrate
python manage.py cratesuperuser

create a superuser following the CLI prompts and then you can explore the admin at localhost:8000/admin