Skip to content

It's unclear what connectionOptions is for #267

@tomjaguarpaw

Description

@tomjaguarpaw

Config contains a field called connectionOptions but it's unclear it is for, or at least I can't see how to use it successfully. For example, I thought that I could set connectionOptions to choose a dbname:

> Right db <- startConfig defaultConfig { connectionOptions = mempty { dbname = pure "mydbname" } }
> toConnectionString db
"host=/tmp/tmp-postgres-socket-8892564f dbname=mydbname port=44901"

Although that appears to work no such database called mydbname exists:

$ psql "host=/tmp/tmp-postgres-socket-8892564f dbname=mydbname port=44901"
psql: FATAL:  database "mydbname" does not exist

It seems that the right way to choose a dbname is to use optionsToConfig.

> Right db <- startConfig $ optionsToConfig mempty { dbname = pure "mydbname" }
> toConnectionString db
"host=/tmp/tmp-postgres-socket-4d018f9b dbname=mydbname port=56889"

Then psql shows that the database called mydbname does indeed exist.

$ psql "host=/tmp/tmp-postgres-socket-4d018f9b dbname=mydbname port=56889"
psql (11.7 (Debian 11.7-0+deb10u1))
Type "help" for help.
mydbname=#

(optionsToDefaultConfig also works in place of optionsToConfig)

Is it supposed to be possible to set a dbname by modifying the connectionOptions of a Config? I spent about an hour trying to do so unsuccessfully. The fact that toConnectionString returned a string containing the correct dbname, but that dbname didn't actually exist, sent me down the wrong track. Does my inability to get this to work indicate a bug or have I failed to conjure up the right recipe? I tried to understand the intention of the API by looking at the implementation but I got bamboozled by the Last Monoid stuff.

(Tangentially, even with dbname set a database called postgres still gets created. I don't much care about that, but does it indicate that something has gone wrong somewhere internally?)

$ psql "host=/tmp/tmp-postgres-socket-4d018f9b dbname=mydbname port=56889"
psql (11.7 (Debian 11.7-0+deb10u1))
Type "help" for help.

mydbname=# \l
                              List of databases
   Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
 mydbname  | tom   | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 |
 postgres  | tom   | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 |
 template0 | tom   | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/tom           +
           |       |          |             |             | tom=CTc/tom
 template1 | tom   | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/tom           +
           |       |          |             |             | tom=CTc/tom
(4 rows)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions