Skip to content

Document connection strings for unix sockets with postgres #258

Closed

Description

@lf-

I am using tmp-postgres for Haskell which creates a Unix socket with a connection string like host=/tmp/tmp-postgres-socket-33d7937af9c67679 dbname=postgres port=49905.

I want to use refinery-cli to apply my migrations to such a database.

According to the postgres docs, the connection string can be in several formats, which do support Unix sockets: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING

However, it seems that Refinery parses them itself:

Ok(Self {
main: Main {
db_type,
db_path: Some(
url.as_str()[url.scheme().len()..]
.trim_start_matches(':')
.trim_start_matches("//")
.to_string()
.into(),
),
db_host: url.host_str().map(|r| r.to_string()),
db_port: url.port().map(|r| r.to_string()),
db_user: Some(url.username().to_string()),
db_pass: url.password().map(|r| r.to_string()),
db_name: Some(url.path().trim_start_matches('/').to_string()),
#[cfg(feature = "tiberius-config")]
trust_cert,
},
})

It seems like I need to generate a connection string like so, but this was obtained largely by guessing:

{- | Serializes a 'PG.Options' into the right format for Refinery CLI, which is

 > postgres://user:pass@(urlencoded host)/db

 See <https://github.com/rust-db/refinery/blob/36e2c219de236c95853a0aa6cd606a2d52d255f8/refinery_core/src/config.rs#L225-L243>
-}
optionsToUri :: PG.Options -> Maybe (URIRef Absolute)
optionsToUri PG.Options {host, user, password, dbname, port} = do
  host' <- getLastBS host
  dbname' <- getLastBS dbname
  port' <- getLast port
  -- Postgres will be grumpy if you don't give it a user, so there's not any
  -- reasonable default here
  user' <- getLastBS user
  pure $
    URI
      { uriScheme = Scheme "postgres"
      , uriAuthority =
          Just $
            Authority
              { authorityUserInfo =
                  Just $ UserInfo user' (fromMaybe "" $ getLastBS password)
              , authorityHost = Host (cs . toLazyByteString $ urlEncode [] host')
              , authorityPort = Just $ Port port'
              }
      , uriPath = "/" <> cs dbname'
      , uriQuery = Query []
      , uriFragment = Nothing
      }
  where
    getLastBS :: Last String -> Maybe ByteString
    getLastBS s = cs <$> getLast s

Activity

  1. jxs commented on Nov 25, 2022

    @jxs
    Member

    Hi, and thanks for the Interest! So, yeah refinery assumes you pass a connection uri, defined in the doc you linked above .
    If I understand correctly it worked after you did so right? Would you be willing to submit a PR improving the documentation to make users aware of it?
    Thanks!

  2. lf- commented on Nov 25, 2022

    @lf-
    Author

    that's the thing: it does not parse those as far as i can tell. it only takes the one format that i guessed by reading the code since it does the parsing itself rather than giving the whole thing to postgres.

    if i remember, I'll look into filing a pr on Monday

  3. jxs commented on Nov 25, 2022

    @jxs
    Member

    yeah, but it then returns a connection string to postgres, see here should have the same format as the input doesn't it on your case?

  4. lf- commented on Nov 25, 2022

    @lf-
    Author

    Ah. Yes, but most of the formats supported by postgres are thus unsupported. For instance refinery seemingly discards query parameters.

  5. jxs commented on Nov 25, 2022

    @jxs
    Member

    Ah, I see. I will gladly support them for the drivers that support them for the input urls, if you want to submit a PR :)

  6. github-actions commented on Mar 31, 2026

    @github-actions

    This issue has been automatically marked as stale due to inactivity.

  7. lf- commented on Mar 31, 2026

    @lf-
    Author

    I HATE STALE BOT. but also, yeah no this is still a bug, i wrote a very dubious socat workaround for this last week. unfortunately i have no time to fix it properly :(

  8. github-actions commented on May 3, 2026

    @github-actions

    This issue has been automatically marked as stale due to inactivity.

  9. lf- commented on May 3, 2026

    @lf-
    Author

    MISCLICKED. This is still a bug.

  10. jxs commented on May 5, 2026

    @jxs
    Member

    I HATE STALE BOT. but also, yeah no this is still a bug, i wrote a very dubious socat workaround for this last week. unfortunately i have no time to fix it properly :(

    Hi, I feel this is more of a lack of feature and not a bug.
    The issue was closed as it didn't have progress, but you very welcome to submit a PR to address this.
    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions