Method: PG::Connection#initialize

Defined in:
ext/pg_connection.c

#initialize(*args) ⇒ Object

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.

host

server hostname

hostaddr

server address (avoids hostname lookup, overrides host)

port

server port number

dbname

connecting database name

user

login user name

password

login password

connect_timeout

maximum time to wait for connection to succeed

options

backend options

tty

(ignored in newer versions of PostgreSQL)

sslmode

(disable|allow|prefer|require)

krbsrvname

kerberos service name

gsslib

GSS library to use for GSSAPI authentication

service

service name to use for additional parameters

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 )

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/pg_connection.c', line 181

static VALUE
pgconn_init(int argc, VALUE *argv, VALUE self)
{
	PGconn *conn = NULL;
	VALUE conninfo;
	VALUE error;

	conninfo = rb_funcall2( rb_cPGconn, rb_intern("parse_connect_args"), argc, argv );
	conn = gvl_PQconnectdb(StringValuePtr(conninfo));

	if(conn == NULL)
		rb_raise(rb_ePGerror, "PQconnectdb() unable to allocate structure");

	Check_Type(self, T_DATA);
	DATA_PTR(self) = conn;

	if (PQstatus(conn) == CONNECTION_BAD) {
		error = rb_exc_new2(rb_eConnectionBad, PQerrorMessage(conn));
		rb_iv_set(error, "@connection", self);
		rb_exc_raise(error);
	}

#ifdef M17N_SUPPORTED
	pgconn_set_default_encoding( self );
#endif

	if (rb_block_given_p()) {
		return rb_ensure(rb_yield, self, pgconn_finish, self);
	}
	return self;
}