Dear DigitalOcean Support Team,
I am planning to set up two separate droplets on DigitalOcean and need guidance on connecting them securely:
I want the Django app to connect to the PostgreSQL database on the other droplet. Could you please provide guidance or best practices on:
I would appreciate any sample configurations or documentation links that can help me set this up efficiently.
Thank you for your support.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya,
Here is a guide on how to achieve that, hope it helps!
The most secure approach is to place both droplets in the same VPC with private networking:
On your PostgreSQL droplet, edit the configuration files:
/etc/postgresql/[version]/main/postgresql.conf:
# Listen on private IP only (not 0.0.0.0)
listen_addresses = 'localhost,10.x.x.x' # Replace with your DB droplet's private IP
/etc/postgresql/[version]/main/pg_hba.conf:
# Allow Django droplet's private IP to connect
host all all 10.x.x.x/32 scram-sha-256 # Replace with Django droplet's private IP
Restart PostgreSQL:
sudo systemctl restart postgresql
Option A: DigitalOcean Cloud Firewall (Recommended)
Option B: UFW (Ubuntu Firewall) On the PostgreSQL droplet:
sudo ufw allow from 10.x.x.x to any port 5432 # Django droplet's private IP
sudo ufw enable
In your Django settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_database_name',
'USER': 'your_db_user',
'PASSWORD': 'strong_password',
'HOST': '10.x.x.x', # PostgreSQL droplet's PRIVATE IP
'PORT': '5432',
}
}
✅ DO:
postgres superuser)sslmode='require' in Django settings)❌ DON’T:
0.0.0.0 (public internet)For encrypted connections, configure PostgreSQL SSL:
# postgresql.conf
ssl = on
ssl_cert_file = '/path/to/server.crt'
ssl_key_file = '/path/to/server.key'
Update Django settings:
'OPTIONS': {
'sslmode': 'require',
}
From your Django droplet, test connectivity:
# Install PostgreSQL client
sudo apt-get install postgresql-client
# Test connection using private IP
psql -h 10.x.x.x -U your_db_user -d your_database_name
Heya, @4ae272398ee6445a9bf73f4cd632b5
You can put both droplets (Django app and PostgreSQL) in the same VPC and region. Then use the PostgreSQL droplet’s private VPC IP as HOST in your Django DATABASES settings. On the PostgreSQL server, allow connections only on that private IP and open port 5432 only to the app droplet (via Cloud Firewall and/or UFW).
That way, all DB traffic stays on the private VPC network (never touches the public internet), and only your Django droplet can reach the database.
Regards
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.