Skip to content

Latest commit

 

History

History
93 lines (63 loc) · 2.18 KB

postgresql_setup_in_django.md

File metadata and controls

93 lines (63 loc) · 2.18 KB

Practical Steps to Set Up PostgreSQL in a Django Project

Step 1: Install psycopg2

Install psycopg2, the PostgreSQL adapter for Python, by running the following command in your terminal:

pip install psycopg2

You can also use psycopg2-binary for a precompiled version:

pip install psycopg2-binary

Step 2: Create a New Database

  1. Log into PostgreSQL as a superuser:

    psql -U postgres
  2. Create a new database (replace my_database with your desired database name):

    CREATE DATABASE my_database;
  3. Exit the PostgreSQL prompt:

    \q

Step 3: Create a New Role

  1. Log back into PostgreSQL:

    psql -U postgres
  2. Create a new role (replace my_user and my_password with your desired username and password):

    CREATE ROLE my_user WITH LOGIN PASSWORD 'my_password';
  3. Grant the new role privileges to create databases and connect to the existing database:

    ALTER ROLE my_user CREATEDB;
    GRANT CONNECT ON DATABASE my_database TO my_user;
  4. Exit the PostgreSQL prompt:

    \q

Step 4: Connect the Role to the Database in Django

  1. Open your Django project and locate the settings.py file.

  2. Update the DATABASES setting to include your new database and role:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'my_database',      # Your new database name
            'USER': 'my_user',          # Your new role name
            'PASSWORD': 'my_password',   # Your new role password
            'HOST': 'localhost',         # Or your database host
            'PORT': '5432',              # Default PostgreSQL port
        }
    }

Step 5: Apply Migrations

Run the following command to apply migrations and set up the database schema:

python manage.py migrate

Conclusion

You have successfully set up PostgreSQL in your Django project, created a new database, created a new role, and connected that role to the database. If you encounter any issues, check the console output for error messages and ensure that your PostgreSQL service is running.