Re: Libpq functions & string to obtain connection parameters

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Libpq functions & string to obtain connection parameters
Date: 2016-10-07 13:13:18
Message-ID: 20161007131318.GA7016@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

arnaud gaboury <arnaud(dot)gaboury(at)gmail(dot)com> wrote:

> I start an app which use a libpq function to read connection params in a URI.
> Until now, the app was using a tcp connection to connect to postgresql with
> this URI:
>
> postgres://mmuser:mmuser_password(at)10(dot)10(dot)10(dot)1:5432/mattermost?sslmode=disable&
> connect_timeout=10
>
> I want to switch and make the app connect to the unix socket instead. I am
> trying to figure out what would then be the correct URI, with no luck until
> now.

The general form for a connection URI is:

postgresql://[user[:password](at)][netloc][:port][/dbname][?param1=value1&...]

The URI scheme designator can be either postgresql:// or postgres://.
Each of the URI parts is optional. The following examples illustrate
valid URI syntax uses:

postgresql:// postgresql://localhost postgresql://localhost:5433
postgresql://localhost/mydb postgresql://user(at)localhost
postgresql://user:secret(at)localhost
postgresql://other(at)localhost/otherdb?connect_timeout=10&application_name=myapp

Components of the hierarchical part of the URI can also be given as
parameters. For example:

postgresql:///mydb?host=localhost&port=5433

Percent-encoding may be used to include symbols with special meaning in
any of the URI parts.

Any connection parameters not corresponding to key words listed in
Section 31.1.2 are ignored and a warning message about them is sent to
stderr.

For improved compatibility with JDBC connection URIs, instances of
parameter ssl=true are translated into sslmode=require.

The host part may be either host name or an IP address. To specify an
IPv6 host address, enclose it in square brackets:

postgresql://[2001:db8::1234]/database

The host component is interpreted as described for the parameter host.
In particular, a Unix-domain socket connection is chosen if the host
part is either empty or starts with a slash, otherwise a TCP/IP
connection is initiated. Note, however, that the slash is a reserved
character in the hierarchical part of the URI. So, to specify a
non-standard Unix-domain socket directory, either omit the host
specification in the URI and specify the host as a parameter, or
percent-encode the path in the host component of the URI:

postgresql:///dbname?host=/var/lib/postgresql

postgresql://%2Fvar%2Flib%2Fpostgresql/dbname

does that help?

(Copy&Paste from
http://stackoverflow.com/questions/27037990/connecting-to-postgres-via-database-url-and-unix-socket-in-rails)

Regards, Andreas Kretschmer
--
Andreas Kretschmer
http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2016-10-07 13:36:12 Re: Lock contention in TransactionIdIsInProgress()
Previous Message arnaud gaboury 2016-10-07 13:01:19 Libpq functions & string to obtain connection parameters