Sending Data from Flask to PostgreSQL

Now let us test the entire script I wrote and if we can insert some data into the database using it, First run the Flask App shown before then make a POST request to /create a route using a tool like Postman.

postman screenshot

The response above looks fine let’s see if there is any entry in the user’s table or not.

Now go to your PostgreSQL shell and Type 

SELECT * FROM users;

postgres output



Sending data from a Flask app to PostgreSQL Database

A database is used to store and maintain persistent data that can be retrieved and manipulated efficiently. we usually need a database, an organized collection of data for example in an e-commerce web app where we need to store the data about the users, orders, carts, shipping, etc. In a database, data is stored in such a way that it makes data transactions very efficient.

In this article, we will cover how we can configure a PostgreSQL database with a Flask app and store some data in the database after configuring it. Now before directly moving to the configuration phase here is a short overview of all tools and software we will use.

Python is a prevalent programming language that we will be using in this article. Flask is a lightweight Python web framework that provides valuable tools and features for creating web applications in Python. PostgreSQL or Postgres is a powerful, open-source object-relations database system. We are using psycopg2 which is a PostgreSQL database adapter that allows you to interact with the PostgreSQL database in Python.

 Prerequisites

  • A decent understanding of Python and a machine with Python installed
  • Understanding of basic concepts of Flask
  • Postgres is installed on your local machine

Similar Reads

Creating a Database and User

Now we set up PostgreSQL by creating a database with a user, and it is granted all required privileges on the created database....

Creating a Flask App

We are done with the database setup now we will create a Flask app and connect it to the database we created and insert some user data into it. First, let’s create a Flask app as follows....

Connecting Flask App to Database

...

Sending Data from Flask to PostgreSQL

We have got a running starter Flask App with some basic code let’s connect it to the database using the following script....