Connecting to Postgresql

I am trying to connect postgresql to my streamlit application.

I am running this locally.
Here is the code block

import streamlit as st

# Initialize connection.
conn = st.connection("postgresql", type="sql")

# Perform query.
df = conn.query('SELECT * FROM user_table;', ttl="10m")

# Print results.
for row in df.itertuples():
    st.write(f"{row.person}")

Here is my secret.toml file contents:

[connections.postgresql]
dialect = "postgresql"
host = "localhost"
port = "5432"
database = "main"
username = "postgres"
password = "1234"

Below is the error I am facing:

AttributeError: module 'sqlalchemy.engine' has no attribute 'URL'
Traceback:
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
File "C:\Users\xxx\streamlit-database.py", line 4, in <module>
    conn = st.connection("postgresql", type="sql")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\connection_factory.py", line 322, in connection_factory
    conn = _create_connection(
           ^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\metrics_util.py", line 396, in wrapped_func
    result = non_optional_func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\connection_factory.py", line 104, in _create_connection
    return __create_connection(name, connection_class, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 212, in wrapper
    return cached_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 241, in __call__
    return self._get_or_create_cached_value(args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 267, in _get_or_create_cached_value
    return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 321, in _handle_cache_miss
    computed_value = self._info.func(*func_args, **func_kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\runtime\connection_factory.py", line 82, in __create_connection
    return connection_class(connection_name=name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\connections\base_connection.py", line 71, in __init__
    self._raw_instance: Optional[RawConnectionT] = self._connect(**kwargs)
                                                   ^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\xxx\.conda\envs\env\Lib\site-packages\streamlit\connections\sql_connection.py", line 103, in _connect
    url = sqlalchemy.engine.URL.create(
          ^^^^^^^^^^^^^^^^^^^^^

Below are the versions im using:
Python : 3.11.0
Streamlit: 1.30.0

Which version of sqlalchemy?

1 Like

Its 1.3.22

After upgrading SQLAlchemy to the latest version, sqlalchemy-2.0.27, it executed without any issues.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.