class PG::Connection
def new(*args)
This is because in non-timeout cases the host is tried multiple times.
It's still possible to do load balancing with +load_balance_hosts+ set to +random+ and to increase the number of connections a node gets, when the hostname is provided multiple times in the host string.
- When a timeout occurs due to the value of +connect_timeout+, then the given +host+, +hostaddr+ and +port+ combination is not tried a second time, even if it's specified several times.
When a host resolves to more than one address, it is therefore tried more often than a host that has only one address.
This means that when +load_balance_hosts+ is set to +random+, then all resolved addresses are tried randomly in one level.
- All hosts are resolved before the first connection is tried.
The {details to libpq}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS] describe how it works, but there are two small differences how ruby-pg handles multiple hosts:
In the Keyword/Value format, the host, hostaddr, and port options accept comma-separated lists of values.
It is possible to specify multiple hosts to connect to, so that they are tried in the given order or optionally in random order.
=== Specifying Multiple Hosts
PG::Connection.new( "postgresql://user:pass@pgsql.example.com:5432/testdb?sslmode=require" )
# As an URI
PG::Connection.new( nil, 5432, nil, nil, 'test', nil, nil )
# As an Array
PG::Connection.new( "dbname=test port=5432" )
# As a String
PG::Connection.new( dbname: 'test', port: 5432 )
# As a Hash
PG::Connection.new
# Connect using all defaults
=== Examples:
Raises a PG::Error if the connection fails.
connection will have its +client_encoding+ set accordingly.
If the Ruby default internal encoding is set (i.e.,
Encoding.default_internal != nil
), thelogin password
[+password+]
login user name
[+user+]
connecting database name
[+dbname+]
(ignored in all versions of PostgreSQL)
[+tty+]
backend options
[+options+]
server port number
[+port+]
server hostname
[+host+]
The positional parameter form has the same functionality except that the missing parameters will always take on default values. The parameters are:
See the documentation of {connection strings}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING].
There are two accepted formats for +connection_string+: plain
keyword = value
strings and URIs.See the {list of valid parameters}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS] in the PostgreSQL documentation.
+connection_hash+ must be a ruby Hash with connection parameters.
=== Create a connection to the specified server.
PG::Connection.new(host, port, options, tty, dbname, user, password) -> conn
PG::Connection.new(connection_string) -> conn
PG::Connection.new(connection_hash) -> conn
PG::Connection.new -> conn
call-seq:
def new(*args) conn = connect_to_hosts(*args) if block_given? begin return yield conn ensure conn.finish end end conn end