PostgreSQL – Create table using Python
This article explores the process of creating table in The PostgreSQL database using Python.
- psycopg2 module
- sample database
Creating a Table:
To create a table in the database use the following steps:
- First create a CREATE TABLE statement
- Second establish a connection to the database using the connect() function
- Third construct a cursor object by using the cursor() method.
- Now execute the above created CREATE TABLE statement using the execute function.
In this example we have already created a Database called school. We will be adding tables to it. To do so we created a file called create_table.py and defined a create_table() function as shown below:
This will successfully create the tables :
To verify so use the below command through the client tool of the same database(ie, school):
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course