Felix Horvat
05/12/2022, 9:49 AMAnna Geller
05/12/2022, 11:42 AMexecute = PostgresExecute(db_name="", user="", ...)
with Flow() as flow:
pwd = PrefectSecret("POSTGRES_DB_PASSWORD")
execute(password=pwd)
Dylan Lim
05/20/2022, 6:28 AMpsycopg2
and run the flow on my local machine, it works. Could anyone help me?
For context, I’m testing a simple write to a postgres database; it’s a single-row record
Task using PostgresExecute: (this fails with FATAL: password authentication failed for user …, SSL off)
def load(record):
pwd = PrefectSecret(PASSWORD)
try:
conn = PostgresExecute()
conn.run(db_name="name", user=...)
return
except Exception as e:
raise e
Task using psycopg2 (this works):
def load(record):
try:
conn = psycopg2.connect(user="admin", password=...)
cursor = conn.cursor()
query = """INSERT INTO test_table (col) values (%s)"""
values = (record,)
cursor.execute(query, values)
conn.commit()
except Exception as e:
raise e
finally:
if conn:
cursor.close()
conn.close()
Anna Geller
05/20/2022, 1:51 PMpwd = PrefectSecret(PASSWORD)
#2 You shouldn't have to use try-except blocks when using Prefect
#3 Usually you would initialize the task and then call it within your Flow, doing .run() is supported but discouraged
conn.run(db_name="name", user=...)
from prefect.client import Secret
...
def load(record):
pwd = Secret(PASSWORD).get() # this way you retrieve it directly
try:
conn = PostgresExecute()
conn.run(db_name="name", user=...)
return
except Exception as e:
raise e
Dylan Lim
05/20/2022, 3:11 PMmutation {
set_secret( input: {
name: "PASSWORD",
value: "PASSWORD"
} ) {
success
}
}
Response:
{
"errors": [
{
"path": [
"set_secret"
],
"message": "Expected type JSON!, found \"PASSWORD\"; Expecting value: line 1 column 1 (char 0)",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"message": "Expected type JSON!, found \"PASSWORD\"; Expecting value: line 1 column 1 (char 0)",
"locations": [
{
"line": 2,
"column": 45
}
],
"path": null
}
}
}
],
"data": {
"set_secret": null
}
}
I saw in a separate thread that you mentioned that this should only occur when using server and not cloud - but I’m using this on the cloud interface and still get this errorAnna Geller
05/20/2022, 3:18 PMDylan Lim
05/20/2022, 3:22 PMAnna Geller
05/20/2022, 3:22 PM