Django + PostgreSQL

Make a Data-Driven App

Develop powerful Python web applications with a Django server and managed PostgreSQL database. When you launch this bundle you’ll deploy a Droplet pre-configured with Django and attached to a managed PostgreSQL database.

Use Cases

  • Blogging Systems
    • Django’s built-in admin panel is ideal for content management, allowing administrators to easily manage articles, images, and user comments. A managed PostgreSQL database provides reliability and scaling options as traffic grows.
  • E-commerce Platforms
    • Django’s modular structure is suited for integrating payment gateways, managing products, and processing orders. PostgreSQL’s support for complex queries and ACID compliance ensures consistent transactions, making it well-suited for storing product details, order history, and transaction records.
  • Real-Time Web Applications
    • Django combined with channels (for WebSockets) and a managed PostgreSQL database can handle real-time updates. PostgreSQL’s support for advanced concurrency and pub/sub notifications (LISTEN/NOTIFY) enables real-time data handling in a robust environment.

Software Included

Package Version License
Django 5.0 Custom
Nginx 1.18.0 Custom
Certbot 0.40.0 Apache 2
Gunicorn 20.0.4 MIT
Postgres 12 PostgreSQL
Postfix 3.4.10 IBM Public

Deploying this Offering using the Control Panel

Click the Deploy to DigitalOcean button to deploy this offering. If you aren’t logged in, this link will prompt you to log in with your DigitalOcean account.

Deploy Django + PostgreSQL to DO

Getting Started After Deploying Django + PostgreSQL

After you’ve created a Droplet, you can navigate to its public IPv4 address to see the sample application live.

To learn how to make modifications or get your code onto your Droplet, here are the steps. You can also follow the sample application for the instructions.

Step 1: Access to your Droplet

Open a terminal on your computer to access your Droplet as the root user using the following command:

ssh root@your_droplet_public_ipv4

You will then be prompted to enter a password. If you created your Droplet with a root user password, enter this in the terminal. If you created your Droplet with an ssh key, enter the passphrase associated with your key.

Step 2: Get your code on here

Note the login message, it has important details for connecting to your Postgres database, among other things!

Clone your Django code onto the droplet, anywhere you like. Note: If you’re not using a source control, you can directly upload the files to your droplet using SFTP.

You can try to reuse this project, located in /home/django/django_project, or start fresh in a new location and edit Gunicorn’s configuration to point to it at /etc/systemd/system/gunicorn.service. You can also change how nginx is routing traffic by editing /etc/nginx/sites-enabled/default.

cd into the directory where your Django code lives, and install any dependencies. (For example, if you have a requirements.txt file, run pip install -r requirements.txt.)

That’s it! Whenever you make code changes, reload Gunicorn like so:

PID=$(systemctl show --value -p MainPID gunicorn.service) && kill -HUP $PID

Step 3: Play in the admin area

The standard Django admin area is accessible at /admin. The login and password are stored in the DJANGO_USER* values you see when you call cat /root/.digitalocean_passwords while logged in over SSH.

Step 4: Get production-ready

There’s a lot you’ll want to do to make sure you’re production-ready. Here are the popular things that people will do.

  • Firewall: Review your firewall settings by calling sudo ufw status, and make any changes you need. By default, only SSH/SFTP (port 22), HTTP (port 80), and HTTPS (port 443) are open. You can also disable this firewall by calling sudo ufw disable and use a DigitalOcean cloud firewall instead, if you like (they’re free).
  • Domain: Register a custom domain.
  • Storage: You can mount a volume (up to 16TB) to this server to expand the filesystem, provision a database cluster (that runs MySQL, Redis, or PostgreSQL), or use a Space, which is an S3-compatible bucket for storing objects.

Access your Managed Database from your Droplet

To access your DigitalOcean managed PostgreSQL database from your Droplet console, you need to use the psql command in the terminal, providing the database connection details like hostname, port, username, and password which can be found in your DigitalOcean database settings; essentially, you’re connecting to the database remotely from your Droplet using the command line.

Key steps:

  • Find connection details: Navigate to your DigitalOcean dashboard, go to your managed database cluster, and copy the required connection information (hostname, port, username, password).
  • Open Droplet console: Access your Droplet through the DigitalOcean console and open the terminal.
  • Run the psql command: Use the following command structure to connect:
psql -h <database_hostname> -p <database_port> -U <database_username> -d <database_name>
-h: Database hostname 
-p: Database port 
-U: Database username 
-d: Database name