Proyecto uses PostgreSQL as its primary database backend. This guide walks you through installing PostgreSQL and configuring your database connection.
PostgreSQL Installation
Install PostgreSQL
Install PostgreSQL on your system: Ubuntu/Debian
macOS
Windows
sudo apt update
sudo apt install postgresql postgresql-contrib
Start PostgreSQL Service
Ensure PostgreSQL is running: # Ubuntu/Debian
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Check status
sudo systemctl status postgresql
Access PostgreSQL Shell
Switch to the postgres user and access the PostgreSQL shell:
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.
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:
DATABASES = {
'default' : {
'ENGINE' : 'django.db.backends.postgresql_psycopg2' ,
'NAME' : 'Proyecto' ,
'USER' : 'postgres' ,
'PASSWORD' : '1204' ,
'HOST' : 'localhost' ,
'PORT' : '5432' ,
'ATOMIC_REQUESTS' : True ,
}
}
Configuration Parameters
Database backend engine. Proyecto uses django.db.backends.postgresql_psycopg2 for PostgreSQL support.
Database name. Default is Proyecto.
Database user with access privileges. Default is postgres.
Database password for authentication.
HOST
string
default: "localhost"
Database server hostname or IP address.
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:
Make Migrations
Generate migration files for your models: python manage.py makemigrations
Apply Migrations
Apply all pending migrations: This creates all required database tables, including:
Django’s built-in tables (auth, sessions, admin)
CTP application tables
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:
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:
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:
PostgreSQL is running: sudo systemctl status postgresql
PostgreSQL is listening on the correct port: sudo netstat -plnt | grep 5432
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