class PG::Connection


See the PG::Result class for information on working with the results of a query.
# res = conn.exec(‘SELECT 1 AS a, 2 AS b, NULL AS c’)
# Equivalent to:
res = conn.exec_params(‘SELECT $1 AS a, $2 AS b, $3 AS c’, [1, 2, nil])
conn = PG::Connection.open(:dbname => ‘test’)
require ‘pg’
For example, to send query to the database on the localhost:
is recommended, but not necessary.
application programmer’s interface to PostgreSQL. Some familiarity with libpq
, the C
The PostgreSQL connection class. The interface for this class is based on

def self::parse_connect_args( *args )

## for valid arguments.
## Parse the connection +args+ into a connection-parameter string. See PG::Connection.new
def self::parse_connect_args( *args )
	return '' if args.empty?
	# This will be swapped soon for code that makes options like those required for
	# PQconnectdbParams()/PQconnectStartParams(). For now, stick to an options string for
	# PQconnectdb()/PQconnectStart().
	# Parameter 'fallback_application_name' was introduced in PostgreSQL 9.0
	# together with PQescapeLiteral().
	if PG::Connection.instance_methods.find{|m| m.to_sym == :escape_literal }
		appname = $0.sub(/^(.{30}).{4,}(.{30})$/){ $1+"..."+$2 }
		appname = PG::Connection.quote_connstr( appname )
		connopts = ["fallback_application_name=#{appname}"]
	else
		connopts = []
	end
	# Handle an options hash first
	if args.last.is_a?( Hash )
		opthash = args.pop
		opthash.each do |key, val|
			connopts.push( "%s=%s" % [key, PG::Connection.quote_connstr(val)] )
		end
	end
	# Option string style
	if args.length == 1 && args.first.to_s.index( '=' )
		connopts.unshift( args.first )
	# Append positional parameters
	else
		args.each_with_index do |val, i|
			next unless val # Skip nil placeholders
			key = CONNECT_ARGUMENT_ORDER[ i ] or
				raise ArgumentError, "Extra positional parameter %d: %p" % [ i+1, val ]
			connopts.push( "%s=%s" % [key, PG::Connection.quote_connstr(val.to_s)] )
		end
	end
	return connopts.join(' ')
end

def self::quote_connstr( value )

## Quote the given +value+ for use in a connection-parameter string.
def self::quote_connstr( value )
	return "'" + value.to_s.gsub( /[\\']/ ) {|m| '\\' + m } + "'"
end

def conndefaults

## for details.
## Returns an array of Hashes with connection defaults. See ::conndefaults
def conndefaults
	return self.class.conndefaults
end

def copy_data( sql )

"more,csv,data,to,copy\n"
"some,csv,data,to,copy\n"
This prints all rows of +my_table+ to stdout:
end
end
p row
while row=conn.get_copy_data
conn.copy_data "COPY my_table TO STDOUT CSV" do
Example with CSV output format:

This creates +my_table+ and inserts two rows.
end
conn.put_copy_data "more,csv,data,to,copy\n"
conn.put_copy_data "some,csv,data,to,copy\n"
conn.copy_data "COPY my_table FROM STDOUT CSV" do
conn.exec "create table my_table (a text,b text,c text,d text,e text)"
Example with CSV input format:

of #put_copy_data, #get_copy_data and #put_copy_end.
of blocking mode of operation, #copy_data is preferred to raw calls
in case of client side or server side failures. Therefore, in case
This method ensures, that the copy process is properly terminated

connection while the COPY operation is in progress.)
(It is not possible to execute other SQL commands using the same
At this point further SQL commands can be issued via #exec.
so it isn't required to make use of any of them.
is complete. An exception is raised if some problem was encountered,
#copy_data returns another PG::Result object when the data transfer

when finished.
to receive or transmit data rows and should return from the block
The application should then use #put_copy_data or #get_copy_data
PGRES_COPY_IN (depending on the specified copy direction).
is passed to the block, bearing a status code of PGRES_COPY_OUT or
(if there is no error in the command) is a PG::Result object that
This issues the SQL COPY command via #exec. The response to this

Execute a copy process for transfering data to or from the server.

conn.copy_data( sql ) {|sql_result| ... } -> PG::Result
call-seq:
def copy_data( sql )
	res = exec( sql )
	case res.result_status
	when PGRES_COPY_IN
		begin
			yield res
		rescue Exception => err
			errmsg = "%s while copy data: %s" % [ err.class.name, err.message ]
			put_copy_end( errmsg )
			get_result
			raise
		else
			put_copy_end
			get_last_result
		end
	when PGRES_COPY_OUT
		begin
			yield res
		rescue Exception => err
			cancel
			while get_copy_data
			end
			while get_result
			end
			raise
		else
			res = get_last_result
			if res.result_status != PGRES_COMMAND_OK
				while get_copy_data
				end
				while get_result
				end
				raise PG::NotAllCopyDataRetrieved, "Not all COPY data retrieved"
			end
			res
		end
	else
		raise ArgumentError, "SQL command is no COPY statement: #{sql}"
	end
end