Zum Inhalt springen

How to Set Up PostgreSQL for Your Django App on a VPS Server

If you’re hosting your first Django API on a server (like Contabo) and you’ve previously used PostgreSQL locally, you’ll need to configure it on your server too. This guide will walk you through installing and configuring PostgreSQL on your VPS and connecting it to your Django app using your .env file.

Prerequisites

  • A running VPS (e.g., from Contabo) with Ubuntu or Debian.
  • Domain/subdomain already set up.
  • Django project already deployed to the server.
  • .env file with your database credentials ready.
  • SSH access to the server.

Step 1: Install PostgreSQL on Your VPS

SSH into your server:

ssh your-user@your-server-ip

Then install PostgreSQL and its dependencies:

sudo apt update
sudo apt install postgresql postgresql-contrib libpq-dev

Step 2: Create PostgreSQL Database and User

Switch to the postgres user:

sudo -u postgres psql

Run the following SQL commands to create a database and user (update placeholders to match your actual values from .env):

CREATE DATABASE mydb_name;
CREATE USER mydb_user WITH PASSWORD 'strongpassword';
ALTER ROLE mydb_user SET client_encoding TO 'utf8';
ALTER ROLE mydb_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE mydb_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE mydb_name TO mydb_user;
q

Step 3: Configure PostgreSQL for Remote Access (Optional)

Only do this if you need to connect to the DB remotely (e.g., from your local machine or external tools).

Update pg_hba.conf:

sudo nano /etc/postgresql/*/main/pg_hba.conf

Add or update the line:

host    all             all             0.0.0.0/0            md5

Update postgresql.conf:

sudo nano /etc/postgresql/*/main/postgresql.conf

Uncomment and edit:

listen_addresses = '*'

Restart PostgreSQL:

sudo systemctl restart postgresql

Step 4: Configure Django to Use PostgreSQL

Update your .env file in the Django project root:

DB_NAME=mydb_name
DB_USER=mydb_user
DB_PASSWORD=strongpassword
DB_HOST=localhost
DB_PORT=5432

Update your settings.py to use os.environ (if not done already):

import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT', '5432'),
    }
}

Step 5: Install PostgreSQL Client for Django

If you haven’t already installed it:

pip install psycopg2-binary

Step 6: Run Django Migrations

Once the connection is set, apply migrations:

python manage.py makemigrations
python manage.py migrate

You can run the development server to confirm it works:

python manage.py runserver 0.0.0.0:8000

Then visit http://your-server-ip:8000 to confirm everything runs as expected.

Bonus: Secure PostgreSQL (Recommended)

If you’re not using external tools to access the DB:

  • Keep DB_HOST=localhost
  • Avoid opening port 5432 to the world (skip remote config steps)

What’s Next?

Once PostgreSQL is working with Django:

  • Set up Gunicorn and Nginx for production deployment if not earlier done.
  • Use HTTPS with Let’s Encrypt.
  • Set up supervisor to keep your Django app running.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert