Skip to content

Postgres ATTACH ignores explicit alias and fails for database names containing hyphens #45

Description

@steinarts

When attaching a PostgreSQL database whose database name contains a hyphen (-), ATTACH fails even when an explicit alias is provided.

Example:

ATTACH
'host=xxx.xxx.xxx.xxx port=5432 dbname=xy_vip-handball_db user=... password=...'
AS vip_pg
(DBTYPE postgres, SCHEMA='public');

Expected:
internal DuckDB catalog uses vip_pg

Actual:
The extension extracts the PostgreSQL database name and uses that as the internal DuckDB catalog name.

Generated statement:

ATTACH '...'
AS xy_vip-handball_db
(TYPE postgres, SCHEMA public, read_only);

DuckDB then reports
Parser Error: syntax error at or near "-"

Root cause
postgres/src/storage/postgres_storage.cpp

auto catalogName = extractDBName(dbPath);

if (dbName == "") {
dbName = catalogName;
}

connector->connect(dbPath, catalogName, schemaName, clientContext);

Even when an explicit alias (dbName) is supplied, catalogName is always derived from the PostgreSQL connection string.

postgres/src/connector/postgres_connector.cpp

executeQuery(std::format(
"attach '{}' as {} (TYPE postgres, SCHEMA {}, read_only);",
dbPath,
catalogName,
schemaName));

catalogName is interpolated as an unquoted DuckDB identifier.

If the PostgreSQL database is named xy_vip-handball_db
the generated SQL becomes

ATTACH '...'
AS xy_vip-handball_db
(TYPE postgres, SCHEMA public, read_only);

which DuckDB parses as subtraction.

Suggested fix

Either:
use the explicit Ladybug alias (dbName) as the DuckDB catalog name, or
quote and escape the generated DuckDB identifier.

Or both.

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