Sunday, November 30, 2025

Methods to create and question the Python PostgreSQL database



This text is all about creating and querying Python PostgreSQL database in a easy and simple approach.


Moreover, the readers of this text are going to be taught to arrange the surroundings earlier than they’ll create and question the PostgreSQL database in Python.


This text is appropriate for each novices and professionals who’re eager to get their palms on these applied sciences supplied they’re acquainted with relational databases and likewise possess some understanding of recent database instruments and applied sciences.

About Python and PostgreSQL


Allow us to start this text by getting a little bit of a conceptual understanding of each Python and PostgreSQL since we’re going to use and implement these within the upcoming sections of this text.

What’s Python


Python is a strong, object-oriented, and really well-known programming language that ships with a big number of out-of-the-box options. It’s a first-hand alternative for programmers for a number of issues from helper scripts to supporting large-scale purposes.


Python is open-source which implies it’s free to make use of for each industrial and non-commercial tasks on the identical it is vitally straightforward to put in writing code in Python and it is vitally quick to get your code end result in Python.


In keeping with www.python.org among the widespread purposes the place Python is used are as follows:


  1. Automating duties starting from easy to advanced nature

  2. Creating net purposes

  3. Accessing database backends

  4. For in-depth evaluation of information as utilized by information scientists

  5. Accessing and querying databases in python (Python PostgreSQL)

  6. Community programming

  7. Sport growth

  8. Schooling

  9. Scientific computing

  10. Constructing e-commerce techniques (enterprise purposes)


In brief phrases, Python is opening new horizons for its builders and customers together with bettering and increasing its publicity and capabilities to the subsequent stage all these aside from the already attained badge of being free for industrial use (open supply) and simple to program plus to not point out the structure independence it affords.

What’s PostgreSQL


PostgreSQL is yet one more open-source product similar to Python that belongs to the group of relational database techniques.


PostgreSQL makes use of and extends the SQL language to get much more advantages than you get by merely utilizing SQL.


PostgreSQL isn’t a stranger to SQL builders because it follows the identical syntax aside from the extra issues it affords thereby growing the general productiveness of the database system.

What’s Python PostgreSQL


Python and PostgreSQL collectively type a Python PostgreSQL mixture which generally seems to be very productive and environment friendly.


The purpose is to have the ability to handle and manipulate the PostgreSQL database inside Python thereby making it totally accessible and exposing it to the wealthy library of Python for a number of vital duties starting from database entry to information analytics.

The Plan for Python PostgreSQL database creation and querying


On this part, we’re discussing the plan to create and question the PostgreSQL database in Python earlier than we bounce forward.


Please bear in mind we’re going to create a PostgreSQL database natively utilizing the PostgreSQL consumer device (the device supplied by PostgreSQL that helps to construct databases) after which name it inside a Python program adopted by easy manipulation of the database objects however all in Python to show that this mix not solely works however price doing in your skilled endeavors.


So, the abstract of the steps is as follows:


  1. Setup PostgreSQL database system regionally

  2. Setup Python to make use of PostgreSQL regionally (with the assistance of a device)

  3. Construct PostgreSQL database in PostgreSQL

  4. Connect with the PostgreSQL database in Python

  5. Create PostgreSQL database objects (tables) in Python

  6. Populate PostgreSQL database object in Python

  7. Replace the PostgreSQL database object in Python

Setup PostgreSQL database system regionally


PostgreSQL database administration system must be arrange regionally in your machine first. This may be completed by merely putting in PostgreSQL from the official web site and configuring it accurately to run in your machine.


To make use of the Python PostgreSQL database mixture, we should first configure the PostgreSQL database server to create a database there.

Putting in PostgreSQL


Please go to PostgreSQL: Downloads and begin the set up (after the obtain is completed) based mostly in your working system.


On this article we’re putting in PostgreSQL on a home windows PC so our alternative is as follows:


Using Windows to install PostgreSQL software


Set up the required parts aside from pgAdmin4 which isn’t required for this text:


Selecting all components except pgAdmin 4 to install on your machineSelecting all components except pgAdmin 4 to install on your machine


As soon as the setup is completed, please pay money for the next issues:

Server: localhost

Database: Postgres

Port: 5432

Username: postgres

Password: (you selected in the course of the setup however maintain it secure)


Please bear in mind Python PostgreSQL first requires a PostgreSQL server to be put in and configured and that’s precisely what now we have completed now efficiently. PostgreSQL set up comes with a default postgres database.


Nonetheless, we have to take a look at that each one is working nice by operating the PostgreSQL command immediate additionally known as psql terminal.

Testing PostgreSQL Command Immediate (psql)


Kind psql within the home windows search field and you’ll see the PostgreSQL shell able to be began.


Subsequent, begin the psql terminal and supply the configurations to hook up with it (ensuring you might be typing within the right password):


SQL Shell (psql command prompt) server, database, port, username and passwordSQL Shell (psql command prompt) server, database, port, username and password


Upon profitable connection you might be linked to the Postgres database:
Methods to create and question the Python PostgreSQL databaseMethods to create and question the Python PostgreSQL database

Create Pattern Database College


Allow us to now create a brand new pattern database known as College by typing the next code within the command immediate:


Please notice that the semi-colon should be used to finish a SQL assertion in PostgreSQL.


The next output reveals you that the pattern database has been created efficiently.


University database has been created in PostgreSQL.University database has been created in PostgreSQL.

Examine PostgreSQL Databases


Get a listing of all of the databases in PostgreSQL by operating the next command (l):


This reveals us the at the moment current PostgreSQL databases:


All the default databases and newly created database university is showing up.
This is for Python PostgreSQL.All the default databases and newly created database university is showing up.
This is for Python PostgreSQL.


So, we are able to see the pattern database college has been efficiently created in PostgreSQL and we’re preparing for Python PostgreSQL database entry and administration.

Setup Python to make use of PostgreSQL regionally (with the assistance of a device)


To setup, Python to make use of PostgreSQL we have to set up a really helpful open-source device known as Visible Studio Code by clicking the hyperlink Visible Studio Code – Code Enhancing. Redefined.

Set up Python Extension in Visible Studio Code


Python is put in as an extension in Visible Studio Code.


Please open Visible Studio Code and set up the Python extension from Visible Studio Market by clicking the Extension tab and looking out and putting in Python (extension):

Set up PostgreSQL Extension in Visible Studio Code (psycopg2 2.9.5)


Subsequent for Python PostgreSQL database joint entry, we have to set up the PostgreSQL extension in Visible Studio Code.


That is completed by putting in psycopg2 which is a PostgreSQL database adaptor for Python programming language that may assist attainable Python PostgreSQL database connection and querying.


To put in psycopg2 2.9.5 allow us to first create a brand new digital surroundings in Visible Studio code.


Please open the command palette (CTRL+SHIFT+X) and sort Python: Create Setting:


Creating a new virtual environment in Python.Creating a new virtual environment in Python.


It will create a digital surroundings for Python.


Subsequent, open Python Terminal by as soon as once more opening the command palette and choosing the Create Terminal this time adopted by typing the next code within the Terminal:


Upon profitable set up, your Python code is able to discuss with the PostgreSQL database.


Psycopg2 installedPsycopg2 installed

Connect with PostgreSQL database in Python


Subsequent, we have to set up a reference to the PostgreSQL database in Python. Now, to do that, now we have to first create a file that accommodates our PostgreSQL database server credentials that we used previously to hook up with it utilizing the PostgreSQL command immediate.

Create postgresql-university-database.ini file


Please create a PostgreSQL-university-database.ini file that accommodates the connection particulars together with the password of your PostgreSQL database server and put it aside regionally to be accessible by the Python file operating in Visible Studio Code.


It is suggested to create and duplicate this file in the identical location of the Visible Studio Code workspace (the home windows folder that you’re at the moment working in with Visible Studio Code).

Create postgresql_config.py file


As soon as the PostgreSQL-university-database.ini file is saved the subsequent step is to create a config (Python file) file known as postgresql_config.py that calls your ini file.


The file could be positioned within the following GitHub repository:

haroonashraf-python-postgresql-university/postgresql_config.py at essential · haroonashraf/haroonashraf-python-postgresql-university (github.com)

Create postgresql_universitydb_connect.py file


After you have got created the config file you need to create a file to attach in Python utilizing Visible Studio Code in the identical workspace (folder).


Create a file postgresql_universitydb_connect.py.


Please click on the GitHub hyperlink under to entry this file:

haroonashraf-python-postgresql-university/postgresql_universitydb_connect.py at essential · haroonashraf/haroonashraf-python-postgresql-university (github.com)


Run the file to see the next output:


PostgreSQL version 15.1 
Database connection has been closedPostgreSQL version 15.1 
Database connection has been closed

Create PostgreSQL database objects (tables) in Python


We have to now create database objects for the PostgreSQL college database from inside Python (Python PostgreSQL).

Create create_student_subject_tables.py file


After now we have established the connection we have to create the next tables within the database:


  1. Pupil desk

  2. Topic desk


Please create one other Python file in the identical workspace known as create_student_subject_tables.py by utilizing the next hyperlink and run it in Visible Studio Code:

haroonashraf-python-postgresql-university/create_student_subject_tables.py at essential · haroonashraf/haroonashraf-python-postgresql-university (github.com)

Knowledge verify by way of PostgreSQL command immediate


Allow us to verify whether or not the tables have been created or not by operating the next command within the PostgreSQL command immediate:


The output is as follows:


student table
subject table
shown as outputstudent table
subject table
shown as output

Populate PostgreSQL database object in Python (Python PostgreSQL)


Allow us to populate the desk within the PostgreSQL database.

Create insert_four_subjects.py file


Now create and run a file known as insert_four_subjects.py with the assistance of the next GitHub hyperlink:

haroonashraf-python-postgresql-university/insert_four_subjects.py at essential · haroonashraf/haroonashraf-python-postgresql-university (github.com)

Knowledge verify


Examine the info by operating the next command towards the PostgreSQL command shell:


The outcomes are proven under:


All the four subjects we inserted from Python can be seen in psql shell.
1 Data Structure
2 Artificial Intelligence
3 Data Analysis
4 Business IntelligenceAll the four subjects we inserted from Python can be seen in psql shell.
1 Data Structure
2 Artificial Intelligence
3 Data Analysis
4 Business Intelligence

Replace PostgreSQL database object in Python


Lastly, we need to replace the PostgreSQL database by altering the identify of a topic.

Create update_datastructure_subject file


Please create and execute the update_datastructure_Subject.py file with the assistance of the next GithHub repository hyperlink:

haroonashraf-python-postgresql-university/update_datastructure_subject.py at essential · haroonashraf/haroonashraf-python-postgresql-university (github.com)


We’ve changed the topic identify Knowledge Construction with Knowledge Constructions.

Knowledge verify


Allow us to verify it within the PostgreSQL command immediate:


Subject Data Structure has been changed successfully to Data Structures in the PostgreSQL database (university).Subject Data Structure has been changed successfully to Data Structures in the PostgreSQL database (university).


Congratulations, you have got efficiently discovered to create and question the Python PostgreSQL database.

Haroon AshrafHaroon Ashraf
Newest posts by Haroon Ashraf (see all)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles