Skip to content

ipv6 address parsing error in postgresql conn string #838

Closed
@sreenandan

Description

@sreenandan

For ipv6 addresses from https://www.postgresql.org/docs/10/libpq-connect.html we can use square brackets around ipv6 address, but it looks like there is a bug in parsing.

import asyncpg
import asyncio

async def run():
    conn = await asyncpg.connect(f"postgresql://postgres:patron@[fd74:ca9b:3a09:868c:0010:0009:0114:0067]:6321/postgres?sslmode=prefer")
    await conn.close()

asyncio.run(run())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 568, in run_until_complete
    return future.result()
  File "<stdin>", line 2, in run
  File "/usr/local/lib/python3.7/site-packages/asyncpg/connection.py", line 1727, in connect
    max_cacheable_statement_size=max_cacheable_statement_size)
  File "/usr/local/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 656, in _connect
    addrs, params, config = _parse_connect_arguments(timeout=timeout, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 497, in _parse_connect_arguments
    server_settings=server_settings)
  File "/usr/local/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 237, in _parse_connect_dsn_and_args
    host, port = _parse_hostlist(dsn_hostspec, port, unquote=True)
  File "/usr/local/lib/python3.7/site-packages/asyncpg/connect_utils.py", line 197, in _parse_hostlist
    hostlist_ports.append(int(hostspec_port))
ValueError: invalid literal for int() with base 10: 'ca9b:3a09:868c:0010:0009:0114:0067]:6321'`



$ pip show asyncpg
Name: asyncpg
Version: 0.21.0
Summary: An asyncio PostgreSQL driver
Home-page: https://github.com/MagicStack/asyncpg
Author: MagicStack Inc
Author-email: hello@magic.io
License: Apache License, Version 2.0
Location: /usr/local/lib/python3.7/site-packages
Requires: 
Required-by: 

$ postgresql version 13

This works:

async def run():
    conn = await asyncpg.connect(
        user="postgres",
        password="patron",
        host="fd74:ca9b:3a09:868c:0010:0009:0114:0067",
        port=6321,
        database="postgres",
        ssl="prefer"
    )
    await conn.close()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions