You can run your query by psycopg2 that is widely used to use Postgresql database in Python
The code is for connecting to a PostgreSQL database and fetching the result of a query using the Python psycopg2 library:
import psycopg2
# Establish a connection to the database
conn = psycopg2.connect(
    host="host",
    database="database",
    user="user",
    password="password"
)
# Create a cursor object to execute queries
cur = conn.cursor()
# Execute a query to retrieve data
query = "SELECT * FROM table_name"
cur.execute(query)
# Fetch all the rows of the result
rows = cur.fetchall()
# Loop through the rows and print the data
for row in rows:
    print(row)
# Clean up
cur.close()
conn.close()