Three Lessons I Learned About Fitness During the COVID Lockdown

Among the outcomes of COVID-19 was my own selfish issue — a set of fitness plans left unfulfilled and ruined by the gym closures. Fitness was a significant part of my life. I would go to the gym five…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Migrate your Flask App with its PostgreSQL Database from Heroku to Railway

Railway logo

Like all not-six-figure-salary-making aspiring or new developers, I panicked too when I got that unfortunate email from Heroku telling me what are they changing on November 2022. They were going to cease their free hobby tiers and start to charge everybody for each uploaded project on their servers separately. At first, I thought their price wouldn’t be unreasonable, but then I realized that it was not the case, and I started searching for a suitable place to move my apps. After some searching I found Railway. They do the pricing according to the load of your apps on their processors, memories, and servers. The calculation is complicated, but the resulting fee is pretty reasonable for the light usage load of new developer apps when compared with Heroku’s.

They have a free plan with a $5/month credit, 500 hours/month execution limit (around 21 days/mo), and an obligation to link your at-least-90-days-old Github account. If you enter your credit card and switch to Developer Plan, you get unlimited execution time and more system resources, and you still do not pay the first $5 each month. They charge you for the exceeding part of $5 of your use.

In this blog, I will walk through the Railway deployment of a Flask application with a PostgreSQL database deployed already to Heroku. I will use the Railway CLI on the Ubuntu terminal. My Python version is 3.10.

After signing up to Railway, linking our Github account, and preferably entering our credit card information, we click the huge “Create a New Project” section in the middle of the page. Even though there is a “Flask template” option on the next screen, we choose the “Empty Project” because the former tries to make some Github account connections.

New Railway Account Dashboard

There is a (probably more straightforward) deployment method through the Railway Dashboard by connecting to your GitHub account where your app resides, but in this post, I am showing the way with no GitHub interaction.

New Project Menu

After creating the new project, we can change its auto-assigned random name and add a description in the General tab of the Project Settings.

Project Settings

Now it’s time to create our Flask service, but again we choose an “Empty Service” instead of specialized ones.

New Service Types

This time we go to Service Settings and can (1) change our domain name, (2) add a custom domain, (3) change the Service Name.

Even those changes are optional; the one we make under Deploy tab is vital. We write “gunicorn app:app” at Start Command input.

Service Settings — Deploy tab

Next, we create our second service for our PostgreSQL database. From the new service menu, we first choose “Database” and then “Add PostgreSQL”.

Now you created your database service and its variables. We are going back to our Flask service’s Variables tab. We add all referenced PostgreSQL variables from the “Add Reference” drop-down menu.

Flask Service Variables

At this point, our Flask service is connected to our PostgreSQL service, but they both are empty. Next, we start our Ubuntu Terminal to deploy our locally stored app.

As the heading of the post suggests, we assume that we already have a Flask/PostgreSQL app deployed to Heroku, and we probably have the app locally on our computer. But if we have the app only on Heroku, we can clone the app by

We can acquire the required commands by clicking the “Set up your project locally” tab at the bottom left side of the Dashboard screen.

To download and install the Railway CLI:

To login to Railway from the terminal:

From our local project folder, to link to the Railway project:

We can either add the unique project ID at the end of the command above or choose the correct project and service from the prompts after the command execution.

And last send our app up:

The deployment process takes a few minutes to complete. After its completion, if we have a seed.py file, to populate our empty database with required tables, we can use:

I needed this post but couldn’t find it when I was trying to do this migration and spent some hours to finally succeed to deploy my apps. Then I decided to write these instructions to make the process of the people who need the same migration less painful. I hope this post will be helpful for the people who need a step by step instructions for this process.

Add a comment

Related posts:

There Is Nothing Wrong With Me

For the past two years, I have been in therapy. Alongside this, I have been learning how to operate a recording studio for recording, mixing, and mastering my music. A limiter is a tool that flattens…

Success Story of Jeff Bezos. Perhaps no one knew that people would do online shopping in such large numbers.

To get success in life, a lot of hard work has to be done. Jeff Bezos has a lot of hard work attached to his success . Jeff Bezos is an American businessman and entrepreneur who founded Amazon.com…