0

I have python script saved as a text file named test.py on my Desktop.

I need to execute that script using PostgreSQL.

Currently I am trying to create PostgreSQL function:

CREATE FUNCTION database.test()
RETURNS VOID
AS $$
py /C:/Users/User1/Desktop/test.py
$$ LANGUAGE plpython3u

That gives me an error:

ERROR:  SyntaxError: invalid syntax (<string>, line 3)could not compile PL/Python function "test" 

ERROR:  could not compile PL/Python function "test"
SQL state: 38000
Detail: SyntaxError: invalid syntax (<string>, line 3)
from sqlalchemy import create_engine
import pandas as pd


db_config = {'user': '******', 
    'pwd': '*******',
    'host': '*******',
    'port': ****,
    'db': '*******'} 

connection_string = 'postgresql://{}:{}@{}:{}/{}'.format(db_config['user'],
    db_config['pwd'],
    db_config['host'],
    db_config['port'],
    db_config['db'])

engine = create_engine(connection_string) 

test_df = pd.DataFrame(index=[0,1], columns= ['A', 'B'], data = 0)


test_df\
.to_sql(
    'test_2',
    schema='database',
    con=engine,
    if_exists='replace',
    index=False
)

Could someone tell me what am I doing wrong?

5
  • 2
    Does this answer your question? Run Python script from PostgreSQL function Commented Dec 12, 2023 at 14:25
  • Would you paste the python script test.py in the question as well? Commented Dec 12, 2023 at 17:31
  • Do you have the appropriate extension installed? See Documentation. Commented Dec 12, 2023 at 20:09
  • @MiH yeah, posted it Commented Dec 13, 2023 at 10:31
  • @Belayer, Yeah. Installed plpython3u Commented Dec 13, 2023 at 10:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.