Connection to local Postgres - 'utf-8' codec can't decode byte 0xe3 in position 98: invalid continuation byte

Hi,

I am new to Streamlit and trying to see how things go here. I’m testing a Hello World to connect to Postgres, and got stuck in the local connection. Right after showing the Hello World message I tried to access Postgres, but I cannot solve this issue bellow.
I am using Python (version 3.11.5) and Streamlit (version 1.29) installed through Anaconda.
The alication is running locally, not deployed yet.
My code is only:

import streamlit as st
import pandas as pd

st.write("""
# My first app
Hello *world!*
""")

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

# Perform query.
df = conn.query("select version()")

# Print results.
for row in df.itertuples():
    st.write(f"tópico: {row.version}:")`

The output with the error is:

My first app
Hello world!
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 98: invalid continuation byte
Traceback:
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 534, in _run_script
    exec(code, module.__dict__)
File "C:\Users\claud\dashboard.py", line 14, in <module>
    df = conn.query("select version()")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\connections\sql_connection.py", line 238, in query
    return _query(
           ^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 212, in wrapper
    return cached_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\runtime\caching\cache_utils.py", line 241, in __call__
    return self._get_or_create_cached_value(args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\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:\ProgramData\anaconda3\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:\ProgramData\anaconda3\Lib\site-packages\tenacity\__init__.py", line 289, in wrapped_f
    return self(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\tenacity\__init__.py", line 379, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\tenacity\__init__.py", line 314, in iter
    return fut.result()
           ^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\concurrent\futures\_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\concurrent\futures\_base.py", line 401, in __get_result
    raise self._exception
File "C:\ProgramData\anaconda3\Lib\site-packages\tenacity\__init__.py", line 382, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\streamlit\connections\sql_connection.py", line 215, in _query
    instance = self._instance.connect()
               ^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\engine\base.py", line 3264, in connect
    return self._connection_cls(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\engine\base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\engine\base.py", line 3288, in raw_connection
    return self.pool.connect()
           ^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 452, in connect
    return _ConnectionFairy._checkout(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 1267, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 716, in checkout
    rec = pool._do_get()
          ^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\impl.py", line 169, in _do_get
    with util.safe_reraise():
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\util\langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\impl.py", line 167, in _do_get
    return self._create_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 393, in _create_connection
    return _ConnectionRecord(self)
           ^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 678, in __init__
    self.__connect()
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 902, in __connect
    with util.safe_reraise():
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\util\langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\pool\base.py", line 898, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\engine\create.py", line 637, in connect
    return dialect.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\ProgramData\anaconda3\Lib\site-packages\sqlalchemy\engine\default.py", line 615, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\claud\AppData\Roaming\Python\Python311\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

My secrets.toml (located in .streamlit) file is:

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

Streamlit seems to be detecting the secrets.toml file once it is removed Streamlit complains.

Any idea of what is going on?

Cláudio Lima

Welcome to the Streamlit community!

We’re thrilled to have you here. For tips on crafting clear and effective posts, I highly recommend checking out the thread Using Streamlit: How to Post a Question Effectively. This valuable resource will guide you in articulating your issues, ensuring you receive prompt and precise responses from our supportive community members.

Hope it’s ok now. If there is any further ajust let me know.
Tks.
Cláudio

I have not encountered an issue.

import streamlit as st

st.write("""
# My first app
Hello *world!*
""")

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

# Perform query.
df = conn.query("select version()")

# Print results.
for row in df.itertuples():
    st.write(f"tópico: {row.version}:")

image

So, probably there is an issue with my local machine set up, isn’t?

Cláudio

P.s. By reading the error message I am receiving, do you have any suggestion what’s going on with my set up?

Probably. Try a clean installation.

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