Method: PG::Connection.new
- Defined in:
- lib/pg/connection.rb
.new(*args) ⇒ Object Also known as: async_connect, connect, open, setdb, setdblogin
call-seq:
PG::Connection.new -> conn
PG::Connection.new(connection_hash) -> conn
PG::Connection.new(connection_string) -> conn
PG::Connection.new(host, port, options, tty, dbname, user, password) -> conn
Create a connection to the specified server.
connection_hash
must be a ruby Hash with connection parameters. See the list of valid parameters in the PostgreSQL documentation.
There are two accepted formats for connection_string
: plain keyword = value
strings and URIs. See the documentation of connection strings.
The positional parameter form has the same functionality except that the missing parameters will always take on default values. The parameters are:
host
-
server hostname
port
-
server port number
options
-
backend options
tty
-
(ignored in all versions of PostgreSQL)
dbname
-
connecting database name
user
-
login user name
password
-
login password
Examples:
# Connect using all defaults
PG::Connection.new
# As a Hash
PG::Connection.new( dbname: 'test', port: 5432 )
# As a String
PG::Connection.new( "dbname=test port=5432" )
# As an Array
PG::Connection.new( nil, 5432, nil, nil, 'test', nil, nil )
# As an URI
PG::Connection.new( "postgresql://user:[email protected]:5432/testdb?sslmode=require" )
If the Ruby default internal encoding is set (i.e., Encoding.default_internal != nil
), the connection will have its client_encoding
set accordingly.
Raises a PG::Error if the connection fails.
763 764 765 766 767 768 769 770 771 772 773 774 |
# File 'lib/pg/connection.rb', line 763 def new(*args) conn = connect_to_hosts(*args) if block_given? begin return yield conn ensure conn.finish end end conn end |