Deploy Django Project on Vercel With PostgreSQL

Vercel is a cloud platform for static site hosting, serverless functions, and Jamstack deployment. It was formerly known as Zeit and is known for its focus on ease of use and developer experience. With Vercel, developers can deploy their web projects quickly and easily, with features such as automatic HTTPS, a global content delivery network (CDN), and Git integration. The platform is designed for modern web development practices, including static site generation, progressive web apps (PWAs), and modern front-end frameworks. It is a popular choice for developers looking for a streamlined and simple solution for hosting their web projects.
Part 01:
To deploy an existing Django Rest Framework (DRF) project on Vercel, you will need to follow these steps:
- Create a Vercel account if you don’t have one already.
- Clone the DRF project from its repository onto your local machine.
- Create a
vercel.jsonfile in the root of your project directory. This file is used by Vercel to configure your project's deployment settings. The file should contain the following code:
{
"builds": [{
"src": "project_name/wsgi.py",
"use": "@vercel/python",
"config": { "maxLambdaSize": "15mb", "runtime": "python3.9" }
},
{
"src": "build_files.sh",
"use": "@vercel/static-build",
"config": { "distDir": "staticfiles_build" }
}],
"routes": [
{
"src": "/static/(.*)",
"dest": "/static/$1"
},
{
"src": "/(.*)",
"dest": "project_name/wsgi.py"
}
]
}4. Create a build_files.sh file in the root of your project directory. This file will install the required dependencies and run the DRF project in a production environment. The file should contain the following code:
echo "BUILD START"
python3.9 -m pip install -r requirements.txt
python3.9 manage.py collectstatic --noinput --clear
echo "BUILD END"5. Add this line at the end of the file backend/wsgi.py
app= application
6. Add these lines in the backend/settings.py file.
# at the top of file
import os
# At the end of file. add these lines
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_build', 'static')
MEDIA_URLS ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# Also Make aure To set allowed_hosts to '*'
ALLOWED_HOSTS = ['*']
7. Comment these lines in the backend/settings.py file.
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }Because Vercel does not provide a built-in database solution, so you will need to use an external database service such as Amazon RDS, Google Cloud SQL, or MongoDB Atlas.
To use an external database with your Django application on Vercel, you will need to configure your Django settings to connect to the database and specify the database credentials. You can either store the database credentials as environment variables in Vercel or hard-code them into your Django settings.
But after successful deployment, we will attach external rds later on in this article. For now, you can comment.
8. Commit the changes to your repository.
9. Connect your repository to Vercel by going to the Vercel dashboard, selecting your project, and clicking on the “Import Project” button.
10. Choose your repository and select the branch that contains the vercel.json and build_files.sh files.
12. Vercel will now automatically build and deploy your DRF project. You can view the status of the deployment in the Vercel dashboard.
13. Once the deployment is complete, you can access your DRF project by visiting the URL provided by Vercel.
Note: Replace “{{project_name}}” in the vercel.json and build_files.sh files with the actual name of your DRF project.
Part 02:
Now Let’s configure an RDS AWS Database

Amazon Relational Database Service (Amazon RDS) is a fully managed relational database service provided by Amazon Web Services (AWS). It makes it easy to set up, operate, and scale a relational database in the cloud.
To configure an Amazon Web Services (AWS) Relational Database Service (RDS) PostgreSQL database with Django, you need to follow these steps:
a. Launch an RDS instance
b. Configure Django to use the RDS instance
c. Migrate your Django models
d. Test your connection
Launch an RDS instance: Log in to your AWS account and launch an RDS instance for PostgreSQL. Select the appropriate options for your database, such as the engine version, instance type, storage, and security group.
- Configure Django to use the RDS instance: In your Django settings.py file, update the DATABASES setting with the appropriate information for your RDS instance. This should include the engine, name, user, password, host, and port.
#Replace name, Password, Host with your RDS Database name, user, password, host.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'myrdshost.rds.amazonaws.com',
'PORT': '5432',
}
}Where:
database_nameis the name of your database instance on Amazon RDS.database_useranddatabase_passwordare the credentials you created when you set up the database instance.database_hostis the host name for your database instance, which you can find in the Amazon RDS console.database_portis the port number for your database instance, which is typically5432for PostgreSQL.
3. Migrate your Django models: After you’ve updated your settings.py file, you can run the migrate command to create the necessary tables in the RDS instance.and then start using the database in your application.
python manage.py migrate
4. Test your connection: You can now test your connection to the RDS instance by running a query in the Django shell or by visiting a page that accesses the database.
That’s it! You should now be able to use your AWS RDS PostgreSQL instance with your Django application.
You can Also Follow Me on My Social Media Platforms:🤝
Thank You for reading!