Skip to main content
Proyecto uses PostgreSQL as its primary database backend. This guide walks you through installing PostgreSQL and configuring your database connection.

PostgreSQL Installation

1

Install PostgreSQL

Install PostgreSQL on your system:
sudo apt update
sudo apt install postgresql postgresql-contrib
2

Start PostgreSQL Service

Ensure PostgreSQL is running:
# Ubuntu/Debian
sudo systemctl start postgresql
sudo systemctl enable postgresql

# Check status
sudo systemctl status postgresql
3

Access PostgreSQL Shell

Switch to the postgres user and access the PostgreSQL shell:
sudo -u postgres psql
4

Create Database

Create the Proyecto database:
CREATE DATABASE "Proyecto";
The database name is case-sensitive and must match the NAME setting in your Django configuration.
5

Create Database User (Optional)

For production, create a dedicated database user:
CREATE USER proyecto_user WITH PASSWORD 'secure_password';
ALTER ROLE proyecto_user SET client_encoding TO 'utf8';
ALTER ROLE proyecto_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE proyecto_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE "Proyecto" TO proyecto_user;
Replace secure_password with a strong, randomly generated password.

Database Configuration

Configure your database connection in proyecto/settings.py:
proyecto/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'Proyecto',
        'USER': 'postgres',
        'PASSWORD': '1204',
        'HOST': 'localhost',
        'PORT': '5432',
        'ATOMIC_REQUESTS': True,
    }
}

Configuration Parameters

ENGINE
string
required
Database backend engine. Proyecto uses django.db.backends.postgresql_psycopg2 for PostgreSQL support.
NAME
string
required
Database name. Default is Proyecto.
USER
string
required
Database user with access privileges. Default is postgres.
PASSWORD
string
required
Database password for authentication.
HOST
string
default:"localhost"
Database server hostname or IP address.
PORT
string
default:"5432"
PostgreSQL server port.
ATOMIC_REQUESTS
boolean
default:"True"
When set to True, wraps each view in a database transaction. This ensures data consistency by automatically rolling back changes if an exception occurs.
Use environment variables for sensitive database credentials instead of hardcoding them in settings.py.

Install Database Adapter

Install the PostgreSQL adapter for Python:
pip install psycopg2==2.9.5
This package is included in requirements.txt and provides the PostgreSQL database adapter for Django.

Running Migrations

After configuring your database, apply migrations to create the necessary tables:
1

Make Migrations

Generate migration files for your models:
python manage.py makemigrations
2

Apply Migrations

Apply all pending migrations:
python manage.py migrate
This creates all required database tables, including:
  • Django’s built-in tables (auth, sessions, admin)
  • CTP application tables
3

Create Superuser

Create an admin user to access the Django admin interface:
python manage.py createsuperuser

Environment Variables

For production deployments, use environment variables for database credentials:
proyecto/settings.py
import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('DB_NAME', 'Proyecto'),
        'USER': os.environ.get('DB_USER', 'postgres'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'HOST': os.environ.get('DB_HOST', 'localhost'),
        'PORT': os.environ.get('DB_PORT', '5432'),
        'ATOMIC_REQUESTS': True,
    }
}
Set environment variables:
export DB_NAME="Proyecto"
export DB_USER="proyecto_user"
export DB_PASSWORD="your_secure_password"
export DB_HOST="localhost"
export DB_PORT="5432"

Database Backup

Regular backups are critical for data protection. Always test your backup restoration process.

Create a Backup

pg_dump -U postgres -h localhost -d Proyecto > proyecto_backup.sql

Restore from Backup

psql -U postgres -h localhost -d Proyecto < proyecto_backup.sql

Automated Backups

Create a cron job for daily backups:
# Edit crontab
crontab -e

# Add daily backup at 2 AM
0 2 * * * pg_dump -U postgres -d Proyecto > /backups/proyecto_$(date +\%Y\%m\%d).sql
Store backups in a separate location from your database server for disaster recovery.

Connection Testing

Verify your database connection:
python manage.py dbshell
This opens a PostgreSQL shell connected to your configured database. You can run SQL queries to verify connectivity:
\dt  -- List all tables
\q  -- Quit

Troubleshooting

Connection Refused

If you see connection errors, check:
  1. PostgreSQL is running: sudo systemctl status postgresql
  2. PostgreSQL is listening on the correct port: sudo netstat -plnt | grep 5432
  3. pg_hba.conf allows local connections

Authentication Failed

Verify database credentials:
psql -U postgres -d Proyecto -h localhost
If this fails, reset the postgres user password:
sudo -u postgres psql
\password postgres

Database Does Not Exist

Create the database if it doesn’t exist:
sudo -u postgres createdb Proyecto

Build docs developers (and LLMs) love