Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncpg.connect: ValueError: invalid literal for int() with base 10 #1151

Open
dss010101 opened this issue May 7, 2024 · 1 comment
Open

Comments

@dss010101
Copy link

dss010101 commented May 7, 2024

  • asyncpg version: asyncpg==0.29.0
  • PostgreSQL version: Postgres 16 (Azure Postgres Flexible Server)
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    :
  • Python version: 3.12.3
  • Platform: Debian 12.5
  • Do you use pgbouncer?:
  • Did you install asyncpg with pip?: pip install asyncpg
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and
    uvloop?
    :

My connection string looks like this:

asyncpg_connect_str = f'postgres://{db_user}:{db_pwd}@{db_host}/mydb'

Im connecting as follows:

   async def read_sql_async_asyncpg(self, sql, params=None):
      t1 = timeit.default_timer()
      try:
         conn = await asyncpg.connect(self.cfg.asyncpg_connect_str)
         stmt = await conn.prepare(sql)
         columns = [a.name for a in stmt.get_attributes()]
         data = await conn.fetch(sql, *params)
         if not len(data): return None

         df = pd.DataFrame(data, columns=columns)
         self.log.debug(f'read_sql_async sql: {sql}, params:{params}')
         self.log.info(f'perf: {timeit.default_timer() - t1}')
         return df   
      except Exception as exc:
         self.log.exception(exc)         
      finally:
         await conn.close()

The error i am getting is complaining about my password (that is what its highlighting anyway)

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/jtson/.local/lib/python3.12/site-packages/asyncpg/connection.py", line 2329, in connect
    return await connect_utils._connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jtson/.local/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 983, in _connect
    addrs, params, config = _parse_connect_arguments(**kwargs)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jtson/.local/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 692, in _parse_connect_arguments
    addrs, params = _parse_connect_dsn_and_args(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jtson/.local/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 294, in _parse_connect_dsn_and_args
    host, port = _parse_hostlist(dsn_hostspec, port, unquote=True)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jtson/.local/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 229, in _parse_hostlist
    hostlist_ports.append(int(hostspec_port))
                          ^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '<mypwd>'

My password has '!' and another non-ascii character in it in a few places - i wonder if that may be causing it or if its how i have created the connection string above?
Alternatively, the stack trace seems to indicate it wants a port - but I'm using an Azure Postgres Flexible Server - im not sure there is a port available for that...(with psycopg driver, it wasnt necessary to specify a port.)

Any help would be apricated

@elprans
Copy link
Member

elprans commented May 9, 2024

The DSN is a URL and so all parts must be explicitly quoted with urllib.parse.quote

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants