GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Windmill Hub
  • Discord
  • Discussions
  • Windmill
  • Login

This view

  • Original page
  • New address

GeezerVison is showing hub.windmill.dev — Execute arbitrary Query — postgresql script | Windmill Hub

Back to PostgreSQL integration
0

Execute arbitrary Query

by henri186
Published Mar 11, 2024

Execute an arbitrary query on a PostgreSQL resource, more at: https://www.windmill.dev/docs/getting_started/scripts_quickstart/sql

Script postgresql Verified
Use in Windmill

The script

Submitted by henri186 Python3
Verified 571 days ago
All edits
Permalink
1
from typing import TypedDict, Dict, Any
2
import psycopg2
3

4
# Define the PostgreSQL resource type as specified
5
class postgresql(TypedDict):
6
    host: str
7
    port: int
8
    user: str
9
    dbname: str
10
    sslmode: str
11
    password: str
12
    root_certificate_pem: str
13

14
def main(query: str, db_config: postgresql) -> Dict[str, Any]:
15
    # Connect to the PostgreSQL database
16
    conn = psycopg2.connect(
17
        host=db_config["host"],
18
        port=db_config["port"],
19
        user=db_config["user"],
20
        password=db_config["password"],
21
        dbname=db_config["dbname"],
22
        sslmode=db_config["sslmode"],
23
        sslrootcert=db_config["root_certificate_pem"],
24
    )
25

26
    # Create a cursor object
27
    cur = conn.cursor()
28

29
    # Execute the query
30
    cur.execute(query)
31

32
    # Fetch all rows from the last executed statement
33
    rows = cur.fetchall()
34

35
    # Close the cursor and connection
36
    cur.close()
37
    conn.close()
38

39
    # Convert the rows to a list of dictionaries to make it more readable
40
    columns = [desc[0] for desc in cur.description]
41
    result = [dict(zip(columns, row)) for row in rows]
42

43
    return result
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.