Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
697 B
1
Indexable
Never
import psycopg2
import csv

# Conectar a la base de datos
conn = psycopg2.connect(
    host="10.200.0.36",
    port=5432,
    database="witlix",
    user="postgres",
    password="Witlix$$2023",
)

# create a cursor
cur = conn.cursor()

# execute a sql query
cur.execute("SELECT * FROM public.logs")

# fetch the results
results = cur.fetchall()

# open a file in the downloads folder
with open("/tmp/output.csv", "w", newline="") as f:
    # Create a CSV writer
    writer = csv.writer(f)

    # write the column names
    writer.writerow([col[0] for col in cur.description])

    # write the query results
    writer.writerows(results)

# close the cursor and connection
cur.close()
conn.close()