-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresql-app.py
48 lines (38 loc) · 1.28 KB
/
postgresql-app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import psycopg2
# Get the PostgreSQL connection information from environment variables
host = os.environ.get('POSTGRES_HOST')
port = os.environ.get('POSTGRES_PORT')
username = os.environ.get('POSTGRES_USERNAME')
password = os.environ.get('POSTGRES_PASSWORD')
database = os.environ.get('POSTGRES_DB')
table = os.environ.get('TABLE')
column1 = os.environ.get('COLUMN1')
column2 = os.environ.get('COLUMN2')
# Connect to the PostgreSQL database
conn = psycopg2.connect(host=host, port=port, user=username, password=password, dbname=database)
cur = conn.cursor()
# Example data to be inserted
data = [
('John', 'Doe'),
('Jane', 'Smith'),
('Alice', 'Johnson')
]
# SQL statement to insert data into the table
insert_query = f"INSERT INTO {table} ({column1}, {column2}) VALUES (%s, %s)"
# Execute the INSERT statement as well as the SEARCH query for each row of data
cur.executemany(insert_query, data)
# Execute the PostgreSQL search query
search_query = f"SELECT * FROM {table}"
# Execute the PostgreSQL search query
query = f"SELECT * FROM public.{table}"
cur.execute(query)
# Fetch all rows from the query result
rows = cur.fetchall()
# Print the rows to stdout
for row in rows:
print(row)
# Commit the transaction and close the connection
conn.commit()
cur.close()
conn.close()