Can't query using st.connection()

Why do i get a TypeError when using the following code. The error only occurs on my cloud app, but runs fine locally.

Should I rather use psycopg2 to create the connection instead of st.connection()?

conn = st.connection("postgresql", type="sql")
users_df = conn.query("SELECT * from users")
st.dataframe(users_df, use_container_width=True, hide_index=True)
   raise TypeError("Query must be a string unless using sqlalchemy.")
TypeError: Query must be a string unless using sqlalchemy.



Thta is probably a different version of something.

Thanks… I assumed so. Unfortunately, I hadn’t set my package versions and couldn’t figure out which combination works. So for now I’ve changed

‘st.connection()’ to ‘psycopg2.connect()’

and converted all conn.query() to curr.execute(query) :expressionless:

Hi, I had the same issue. I upgraded sqlalchemy to version v.2.0.27 (latest as of today), which fixed this issue.
streamlit==1.31.1, SQLAlchemy==2.0.27

Best, L.

1 Like

Thanks for the update @LStaehli!

Unfortunately, my app was live, so I had already made the change to psycopg2 to get something working.

I’ll have a look at switching back though as I’ve been having random operational errors when using @st_cache_resource on my deployed app (streamlit cloud) when using psycopg2.connect().

Thanks! Updating SQLAlchemy to 2.0.27 solved the issue.