DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Deploy a Django application on Render using PostgreSQL as the database and hosting images over AWS

To deploy a Django application on Render using PostgreSQL as the database and hosting images over AWS S3, follow these steps:

1. Prepare Django Application for Deployment

  • Install required dependencies: Make sure your Django application includes the necessary dependencies in requirements.txt:

    psycopg2-binary
    boto3  # for AWS S3 storage
    dj-database-url  # for parsing the database URL
    gunicorn  # for running the Django application in production
    django-storages  # for S3 storage support
    
  • Configure settings.py:
    Update your settings to connect to PostgreSQL and AWS S3:

    import os
    import dj_database_url
    
    # Render database configuration
    DATABASES = {
        'default': dj_database_url.config(
            default=os.getenv('DATABASE_URL')
        )
    }
    
    # AWS S3 configuration for media storage
    AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
    AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
    AWS_S3_REGION_NAME = os.getenv('AWS_S3_REGION_NAME', 'us-east-1')
    AWS_DEFAULT_ACL = None
    AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
    AWS_S3_OBJECT_PARAMETERS = {
        'CacheControl': 'max-age=86400',
    }
    
    # Static and Media files on S3
    STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
    

2. Set Up PostgreSQL Database on Render

  • Create a PostgreSQL database:
    1. Log in to Render.
    2. Navigate to Databases and click on New PostgreSQL Database.
    3. Set a name for your database and choose a region.
    4. Once created, you will get a DATABASE_URL. Store this as an environment variable in your Render service under Environment with the name DATABASE_URL.

3. Set Up AWS S3 for Image Hosting

  • Create an S3 Bucket:

    1. Log in to the AWS Management Console.
    2. Go to S3 and create a new bucket for storing images.
    3. In Permissions, make sure to set public access appropriately for media files.
  • Set up IAM user for S3 access:

    1. Go to the IAM service in AWS.
    2. Create a new user with programmatic access and attach the AmazonS3FullAccess policy.
    3. Copy the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
  • Configure Django with S3 Credentials:

    1. In Render, navigate to your service settings and add the following environment variables:
      • AWS_ACCESS_KEY_ID
      • AWS_SECRET_ACCESS_KEY
      • AWS_STORAGE_BUCKET_NAME
      • AWS_S3_REGION_NAME

4. Deploy to Render

  • Create a new Web Service:

    1. In Render, go to Dashboard > New > Web Service.
    2. Connect your GitHub repository containing your Django project.
    3. Select a name for your service and pick a region.
    4. For the Build Command, use:

      pip install -r requirements.txt
      python manage.py collectstatic --noinput
      python manage.py migrate
      
5. For the **Start Command**, use:
Enter fullscreen mode Exit fullscreen mode
    ```bash
    gunicorn your_project_name.wsgi:application
    ```
Enter fullscreen mode Exit fullscreen mode

5. Run Migrations and Collect Static Files

Once the deployment is complete, Render will run the migration command and collect static files. If everything is set up correctly, your application should be live with PostgreSQL as the database and AWS S3 hosting your media files.

Let me know if you need any additional details or further clarification!

Top comments (0)