
Python is a highly level programming language that allows you to build general purpose applications. By integrating Python with a Vultr Managed Database for PostgreSQL, you can automate most application processes for fast scaling and high level development of new features to match your user needs.
Depending on your Python application structure, integrating a managed PostgreSQL database best suites the following types of applications:
This guide explains how to use Vultr Managed Database for PostgreSQL in Python applications. By leveraging the database's high availability, you're to create a console application that makes database queries to retrieve stored information.
Before you begin, be sure to:
Deploy a Vultr Cloud Server Distribution of your choice
This guide uses a Linux server, but depending on your Python application backend, you can apply the instructions on any distribution including Windows Servers.
Create a non-root sudo user account and switch to the new account
To integrate Python with a Vultr Managed Database for PostgreSQL, install the required psycopg adapter package that allows you to connect to the database. Depending on your Linux distribution, install the PostgreSQL adapter as described in the steps below.
Update the server:
On Ubuntu/Debian:
On CentOS, and RHEL distributions:
Install the PostgreSQL client tool
OR
Install the Python Pip Package Manager
OR
Using pip, install the PostgreSQL psycopg adapter package
To connect Python to your Vultr Managed Database for PostgreSQL, create a sample database. Within the database, create a table and add sample records you can retrieve using the Python application as described below.
This sample Python application requires a sample PostgreSQL database with one table. This table stores customers' names and their unique customer_ids. Follow the steps below to set up the database:
Using the PostgreSQL client tool psql, log in to the Vultr Managed Database for PostgreSQL cluster
Or, copy and use the connection string in your Vultr Managed Database dashboard
Replace the above psql connection details with your actual database values as below:
vultradminexample-passwordvultr-prod-aaa.com16751When prompted enter the correct Vultr Managed Database for PostgreSQL database to connect to your database.
Create a new xyz_company database
Switch to the new xyz_company database
Create a new sample customers table.
Add sample data to the customers table
View the customers table data to verify that all records are available
Output:
Exit the PostgreSQL database console
In Python, a central database connection module allows you to reuse code in different application files without applying the logic in each file. In this section, create a Python connection file that works as a central module for PostgreSQL connections.
Create a new project directory
Switch to the new directory
Using a text editor such as Nano, create a new Python file postgresql_connector.py
Add the following code to the file. Replace the psycopg.connect values with your actual Vultr Managed Database for PostgreSQL values
Save and close the file.
In the above application file:
PostgresqlConnector() is a class module with five methods:__init__() executes every time you create an instance of the PostgresqlConnector class to establish a connection to the PostgreSQL database using the self.db_conn = psycopg.connect(...) functioninsert_customer(self, query_string, json_data) takes a JSON payload and populates the customers table using the INSERT SQL statementget_customers(self, query_string) retrieve all customers from the databaseupdate_customer(self, query_string, json_data) updates a customer that matches a given customer_id valuedelete_customer(self, query_string, json_data) deletes a customer from the database tableCreate a new main.py file
Add the following code to the file
Save and close the file.
In the above main.py file:
postgresql_connector module you created earlier to the application functionsHttpServerHandler(http.server.SimpleHTTPRequestHandler) is a web server handler class that listens for incoming HTTP connections on your defined port 8080 with the following methods:set_headers(self) sets the correct HTTP headers when the Python application responds to HTTP clientsdo_POST(self) handles all HTTP POST requests to redirect an INSERT query to the PostgresqlConnector() classdo_GET(self) runs an HTTP GET method to retrieve data from the customers table using the PostgresqlConnector() class. This method runs the select * from customers SQL statementdo_PUT(self) executes an HTTP PUT method to update customer details in the database table using the update customers set first_name = %s, last_name = %s where customer_id = %s SQL statementdo_DELETE(self) runs a function that deletes a customer from the database using the delete from customers where customer_id = %s SQL statementYou have developed a Python application with the necessary modules and functions. To verify that your application reads and writes to your Vultr Managed Database for PostgreSQl, run the following operations.
Run the application in the background
Output:
Using the curl utility tool, create a new customer using the following HTTP POST method
Output:
Using the HTTP GET method, retrieve all customers in the database
Output:
Using the PUT method, update a customer's details
Output.
Delete a customer from the database
Output:
To stop the Python application background process, view the running jobs
Output:
Keep note of the background process ID
Stop the process by ID. For example, for job ID 1, run:
You have created a Python application that connects to a Vultr Managed Database for PostgreSQL to perform read and write tasks. Depending on your Python application structure, create the necessary databases, and implement the correct connector with your desired SQL statements to interact with your PostgreSQL database.
To implement more functionalities using your Vultr Managed Database for MySQL, visit the following resources:
0 Comments
Be the first to comment and share your perspective with the community.