Welcome to the world of Django with PostgreSQL on Ubuntu! In this guide, we'll embark on:
- Installing postgreSQL and all requirements.
- Setting up a virtual environment for our project.
- Setting up project.
- Integrating postgres.
Prerequisites:
- Wsl2 and the latest Ubuntu(used Ubuntu 22.04) installed.
- Python(version 3+) installed.
Installing postgreSQL on Ubuntu.
First, refresh your server’s local package index:
sudo apt update
Then, install the postgres package:
sudo apt install postgresql postgresql-contrib
The installation procedure created a user account called postgres that is associated with the default Postgres role. To connectc to the Postgres prompt, run the psql
command as the postgres account directly with sudo
:
sudo -u postgres psql
To exit:
\q
And press exit
to your regular system user.
For more comprehensive information about interacting with Postgres, visit here
Setting up a virtual environment for our project.
First, update Ubuntu by running:
sudo apt update && sudo apt upgrade
Then, install venv by running:
sudo apt install python3-venv.
Create a folder(We are going to name ours Helloworld) by running the following command:
mkdir Helloworld
Inside your HelloWorld project folder, use the following command to create a virtual environment named .venv: python3 -m venv .venv
.
To activate the virtual environment, enter: source .venv/bin/activate
. If it worked, you should see (.venv) before the command prompt.
Open your project folder in VS Code from your Ubuntu terminal by entering: code .
(the "." tells VS Code to open the current folder).
Again if need be, activate the virtual environment that you created using your Bash terminal in VS Code: source .venv/bin/activate
. If it worked, you should see (.venv) before the command prompt.
Setting up project:
Install Django in the virtual environment with the command:
python3 -m pip install django
Next, run the following command to create the Django project:
django-admin startproject myproject .
Note:
Don't run migrations with out setting up a custom user model.
Now, to create a Django app, run the administrative utility's startapp command in your project folder (where manage.py resides):
Refer to this documentation for more reference.
python3 manage.py startapp myuser
Psycopg2 is a PostgreSQL adapter for Python. Install it using pip:
pip install psycopg2
Open your Django project's settings file (settings.py) and modify the DATABASES setting to use PostgreSQL:
from decouple import Config, RepositoryEnv
# Load configuration from .env file
config = Config(RepositoryEnv('.env'))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', cast=bool)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT'),
}
}
Create a .env file in your project's root directory to store sensitive information like database credentials. Add the following entries to it:
DEBUG=True
SECRET_KEY='django-insecure-2$jm=82*r#^p0-b8+0ukno+4!a798awx53rqi!z)h*8*pojr96'
DB_NAME='mydatabase'
DB_USER='myuser'
DB_PASSWORD='your_password'
DB_HOST='localhost'
DB_PORT=''
Replace the values with the appropriate database name, username, password, host, and port.
Python Decouple allows you to manage project settings using environment variables. Install it via pip:
pip install python-decouple
Top comments (0)