xxxxxxxxxx
import psycopg2
# Connect to the database
conn = psycopg2.connect(
host="your_host",
database="your_database",
user="your_user",
password="your_password"
)
# Create a cursor object
cur = conn.cursor()
# Execute the query to get the list of schemas
cur.execute("SELECT schema_name FROM information_schema.schemata")
# Fetch all the rows from the result set
schemas = cur.fetchall()
# Print the list of schemas
for schema in schemas:
print(schema[0])
# Close the cursor and connection
cur.close()
conn.close()
xxxxxxxxxx
import psycopg2
# Connect to the PostgreSQL database
conn = psycopg2.connect(
host="your_host",
port="your_port",
database="your_database",
user="your_user",
password="your_password"
)
# Create a cursor object to interact with the database
cursor = conn.cursor()
# Execute the query to retrieve all schemas from the database
cursor.execute("SELECT schema_name FROM information_schema.schemata")
# Fetch all the results as a list of tuples
schemas = cursor.fetchall()
# Print the list of schemas
for schema in schemas:
print(schema[0])
# Close the cursor and connection
cursor.close()
conn.close()