How To Deploy A Postgres Database For Free | by Cody Nicholson | Towards Data Science

PostgreSQL is one of the most popular relational database management systems in the world powering some of the biggest businesses. If you download Postgres, you may be annoyed to find that you can only host it on your machine’s localhost server unless you pay a recurring fee to host it on a platform like AWS or Azure. In this guide, I’ll show you how you can deploy your database for free using Heroku and write to it in Python.
Install the latest version of Postgres, install pgadmin4, install python3, and pip3 install sqlalchemy and psycopg2
The first step to deploying our free Postgres database is to create a free account on Heroku. Heroku is a cloud platform that you can deploy your apps and databases to. Once you have created your account and logged in, you should see a dashboard screen similar to this:
Your new account won’t have any apps like I have listed above. Find and click the “Create new app” button to get to the following page:
Choose a name for your app that hasn’t already been taken and create it. After clicking the “Create app” button you should get to this page:
You now have your first Heroku app, let’s add a database!
Click the “Resources” tab from your app’s Heroku page to see the empty lists of Dynos and Add-ons your project has. We want to attach the Heroku Postgres add-on to your project, so click the purple “Find more add-ons” button seen on the right of the below picture:
Once on the Heroku Add-ons page, enter “Heroku Postgres” into the “Search Elements” bar on the top right of the page:
You should now have found the Add-on for Heroku Postgres that you can see in the below picture. From here, click the purple “Install Heroku Postgres” button on the top right so we can add it to your newly created app.
On this final page for attaching the Heroku Postgres add-on to your project you’ll be able to select the plan and the project you want to attach the add-on to. Make sure you select the “Hobby Dev - Free” plan so you don’t pay for anything! After, search for your project name, select it, and click the purple “Provision add-on” button.
Let’s learn how to connect to your new Postgres database!
To connect to your new database, return to the “Resources” page of your Heroku app. You should see the attached add-on “Heroku Postgres”. Click the “Heroku Postgres” link on the bottom right of the below picture to go to the database management page.
The Database Overview page shows you high-level information about your database. Initially you should have no connections, rows, or tables. The Data Size starts at 7.9MBs which accounts for the program files related to your database that you won’t ever need to see or worry about. Once you add more data, that size will increase. On the free plan, you are only allows to have 20 connections and 10,000 rows of data in your database — making it very practical for personal projects and experimentation.
If you go to the “Settings” tab and then click the “View Credentials” button you will be able to see the database credentials you’ll use to connect to your database. Do not share this info!
Now that we have our Database Credentials we can connect to pgadmin. Open the pgadmin application you should have installed on your computer and it should open a window in your default browser like this:
From here you can click the “Add New Server” button on the pgadmin dashboard and enter your database credentials into the prompt. You do not need a Role or Service:
Once you’ve entered all the fields correctly you can click “Save” and should see your Postgres server listed like it is below. This is a shared server with many databases and you will only be allowed to access your database by finding it in the list:
You’re now connected to your free Postgres Database in pgadmin!
In this last step we’re going to write python code that will create a table and add data to it in your database. We’ll start by creating a config.py file to store our database credentials. Our database creds should be stored in a separate file so we can add this file to our .gitignore and share our code on GitHub without fear of anyone accessing our database.
host = ec2-34-200-42-57.compute-1.amazonaws.comport = 5432database = dfg5a7fake6perlmsuser = fdpbgfakeggheshfbvpassword = a7cb88511fakea656dd5bb175220caa9646defd4f79e62085486b
The below script create_table.py lives in the same folder as the config.py. First, we import all of the config variables and sqlalchemy. Then we create an engine that takes all of the config variables and uses them to connect to the database using “engine.connect()”. Once we’re connected to the database we grab the database’s metadata so we can modify the schema on the “metadata = MetaData()” line. We’re ready to create our first table!
from sqlalchemy import *from config import host, port, database, user, passwordconn_str = f"postgresql://{user}:{password}@{host}/{database}"engine = create_engine(conn_str)connection = engine.connect()metadata = MetaData()first_tb = Table('first_table', metadata,   Column('id', Integer, primary_key=True),   Column('name', String(255), nullable=False),   Column('isHappy', Boolean, nullable=False))metadata.create_all(engine)query = insert(first_tb).values(id=1, name="Student", isHappy=True)ResultProxy = connection.execute(query)
On the line starting with “first_table” we create our table by passing the table name, the metadata, and all the columns. For each column we can set the name, the type, and the constraints we want each column to have. Finally, on the “metadata.create_all(engine)” line we create our new table in the database. Now that our table is created, we can create a query using the insert() sqlAlchemy method and give it the table and the values we want to add to that table. Lastly, we can execute our query on the last line.
Returning to pgadmin, we can go to: “database_name” -> “Schemas” -> “public” -> “Tables”. If you Right-Click on “first_table” you can select: “View/Edit Data” -> “All Rows”. This will run a select query and return to you the row we added — as seen below!
Thanks for reading! I’d love to hear your feedback or take any questions you have, so please comment below. Please let me know if any of the pictures or information are no longer up to date!