Python PostgreSQL - Drop Table Last Updated : 23 Aug, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to drop tables in PostgreSQL using pyscopg2 module Python. In PostgreSQL DROP TABLE is used to remove the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constraints for that table. If the particular table doesn't exist then it shows an error. Syntax: DROP TABLE table_name; Table Used: Here, we are using the accounts table for demonstration. Now let's drops this table, for we will use will psycopg2 module to connect the PostgreSQL and execute the SQL query in cursor.execute(query) object. Syntax: cursor.execute(sql_query); Example 1: Drop table using psycopg2 Here we are going to drop the table using the DELETE clause. Syntax: DROP TABLE table_name; Code: Python3 # importing psycopg2 import psycopg2 conn=psycopg2.connect( database="test", user="postgres", password="password", host="localhost", port="5432" ) # Creating a cursor object using the cursor() # method cursor = conn.cursor() # drop table accounts sql = '''DROP TABLE accounts ''' # Executing the query cursor.execute(sql) print("Table dropped !") # Commit your changes in the database conn.commit() # Closing the connection conn.close() Output: Table dropped !Example 2: Drop table before checking it is exist or not If you try to delete the same table again, since you have already deleted it, you will get an error saying “table does not exist”, so we can resolve using the IF EXIST clause. Syntax: DROP TABLE table_name IF EXITS table_name; Code: Python3 # importing psycopg2 import psycopg2 conn=psycopg2.connect( database="geeks", user="postgres", password="root", host="localhost", port="5432" ) # Creating a cursor object using the cursor() # method cursor = conn.cursor() # drop table accounts sql = '''DROP table IF EXISTS accounts ''' # Executing the query cursor.execute(sql) print("Table dropped !") # Commit your changes in the database conn.commit() # Closing the connection conn.close() Output: Table dropped ! After execution of the scripts, let check the table in PostgreSQL: Comment More infoAdvertise with us Next Article Python PostgreSQL - Drop Table A annulata2402 Follow Improve Article Tags : Python Python PostgreSQL Python Pyscopg2 Practice Tags : python Similar Reads Python MySQL - Drop Table A connector is employed when we have to use MySQL with other programming languages. The work of MySQL-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. Drop Table Command Drop command affect 2 min read PostgreSQL - Create Tables in Python Creating tables in PostgreSQL using Python is an essential skill for developers working with databases. This article will explore the process of creating new tables in the PostgreSQL database using Python.Why Create PostgreSQL Tables with Python?Using Python to create PostgreSQL tables is beneficial 4 min read PostgreSQL - Create table using Python Creating tables in a PostgreSQL database using Python is a common task for developers working with databases. This process involves defining the structure of your data and ensuring that your database is optimized for efficient storage and retrieval. In this article, we will walk through the steps of 3 min read Python: MySQL Create Table MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify 3 min read PostgreSQL Python - Querying Data Psycopg2 acts as a bridge between Python applications and PostgreSQL databases. Widely employed in diverse Python systems, from web applications to data analysis tools and other software projects, Psycopg2 enables developers to execute queries and manipulate data stored in PostgreSQL databases. In t 5 min read PostgreSQL - DROP VIEWS Statement A view in PostgreSQL can be seen as a virtual table that can contain all rows of a table or selected rows from one or more tables. Views allow us to see limited data instead of the complete information stored in a table. A view can be created from one or many base tables (the table from which the vi 4 min read Python PostgreSQL - Delete Data In this article, we are going to see how to delete data in tables from PostgreSQL using pyscopg2 module in Python. In PostgreSQL, DELETE TABLE is used to delete the data in the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constr 2 min read SQL DROP TABLE The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making 4 min read Python Select from PostgreSQL Table using Psycopg2 This article will introduce you to the use of the psycopg2 module which is used to connect to a PostgreSQL database from Python. We will look over how to establish a connection to a database, create a cursor object to execute DBMS SQL statements, execute a SELECT statement to retrieve the data from 7 min read Python MariaDB - Drop Table using PyMySQL MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of Dropping a table from a database using pymysql. To drop a table use any o 2 min read Like