1

I am trying to execute some Postgres queries with psycopg2.

import psycopg2

conn = psycopg2.connect(
    host="test.postgres.database.azure.com",
    database="postgres",
    user="test",
    password="test")
cur = conn.cursor()

sql = "select site_panel from test.ws_sites"

cur.execute(sql)
rows =  cur.fetchall()

The query above works well and returns the data but the following query does not delete the table as intended.

 cur.execute ("DROP TABLE IF EXISTS test.ws_sites")

Anything wrong I'm doing here?

1 Answer 1

3

A query that modifies table data or schema structure needs to be committed:

cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
conn.commit()

Alternatively, place

conn.autocommit = True

after creating the connection.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.