avatar_url

Sandesh Rana | Python

I'm a developer from Himalayas, with a passion for crafting efficient and elegant code, with creativity and precision.

Setup Django and postgres in archlinux

08 Jun 2023 » linux, django, postgres

This guide will walk you through the process of setting up Django, a popular Python web framework, and PostgreSQL, a powerful open-source relational database management system, on Arch Linux.

Install required packages

Open your terminal and run the following commands to install the necessary packages:

sudo pacman -Sy python python-pip python-virtualenv postgresql

Initialize and activate a virtual environment

Navigate to your project directory and create a virtual environment using virtualenv:

cd /path/to/your/project
virtualenv env

Activate the virtual environment:

source env/bin/activate

Install Django

Install Django within the virtual environment using pip:

pip install django

Configure PostgreSQL

Install PostgreSQL

Install PostgreSQL using pacman:

sudo pacman -Sy postgresql

Initialize the database cluster

sudo -u postgres initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'

Start and enable the PostgreSQL service

sudo systemctl start postgresql
sudo systemctl enable postgresql

Create a PostgreSQL user and database

Access the PostgreSQL prompt:

sudo -u postgres psql

Create a new database:

CREATE DATABASE your_database_name;

Create a new user and grant it privileges on the database:

CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;

Exit the PostgreSQL prompt:

\q

Configure Django settings

Open your Django project’s settings file (settings.py) and update the following settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'your_database_name',
        'USER': 'your_username',
        'PASSWORD': 'your_password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Run database migrations

Now you can run the initial database migrations:

python manage.py migrate

Start the Django development server

You’re all set! Start the Django development server and access your application in your web browser:

python manage.py runserver

That’s it! You have successfully set up Django and PostgreSQL in Arch Linux.

Please note that this guide assumes you have basic knowledge of working with the terminal and the Arch Linux package manager, pacman. Adjust the instructions as necessary based on your specific project requirements and preferences.