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.
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;
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