Class

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inheritance
< ActiveRecord::ConnectionAdapters::AbstractAdapter < Object

The PostgreSQL adapter works both with the native C (ruby.scripting.ca/postgres/) and the pure Ruby (available both as gem and from rubyforge.org/frs/?group_id=234&release_id=1944) drivers.

Options:

  • :host - Defaults to "localhost".
  • :port - Defaults to 5432.
  • :username - Defaults to nothing.
  • :password - Defaults to nothing.
  • :database - The name of the database. No default, must be provided.
  • :schema_search_path - An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the :schema_order option.
  • :encoding - An optional client encoding that is used in a SET client_encoding TO <encoding> call on the connection.
  • :min_messages - An optional client min messages that is used in a SET client_min_messages TO <min_messages> call on the connection.
  • :allow_concurrency - If true, use async query methods so Ruby threads don‘t deadlock; otherwise, use blocking query methods.

Constants

Name   Description
ADAPTER_NAME = 'PostgreSQL'.freeze
NATIVE_DATABASE_TYPES = { :primary_key => "serial primary key".freeze, :string => { :name => "character varying", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "integer" }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "timestamp" }, :timestamp => { :name => "timestamp" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "bytea" }, :boolean => { :name => "boolean" }

Methods

Class

Visibility Signature
public new (connection, logger, connection_parameters, config)

Instance

Visibility Signature
public active? ()
public adapter_name ()
public add_column (table_name, column_name, type, options = {})
public begin_db_transaction ()
public change_column (table_name, column_name, type, options = {})
public change_column_default (table_name, column_name, default)
public change_column_null (table_name, column_name, null, default = nil)
public client_min_messages ()
public client_min_messages= (level)
public columns (table_name, name = nil)
public commit_db_transaction ()
public create_database (name, options = {})
public create_savepoint ()
public current_database ()
public disconnect! ()
public encoding ()
public escape_bytea (value)
public execute (sql, name = nil)
public indexes (table_name, name = nil)
public insert (sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
public outside_transaction? ()
public reconnect! ()
public release_savepoint ()
public remove_index (table_name, options = {})
public rename_column (table_name, column_name, new_column_name)
public rename_table (name, new_name)
public rollback_db_transaction ()
public rollback_to_savepoint ()
public schema_search_path ()
public schema_search_path= (schema_csv)
public select_rows (sql, name = nil)
public supports_ddl_transactions? ()
public supports_insert_with_returning? ()
public supports_migrations? ()
public supports_savepoints? ()
public supports_standard_conforming_strings? ()
public table_alias_length ()
public tables (name = nil)
public type_to_sql (type, limit = nil, precision = nil, scale = nil)
public unescape_bytea (value)
public update_sql (sql, name = nil)
protected postgresql_version ()

Class Method Detail

new(connection, logger, connection_parameters, config)

Initializes and connects a PostgreSQL adapter.

Instance Method Detail

active?()

Is this connection alive and ready for queries?

adapter_name()

Returns ‘PostgreSQL’ as adapter name for identification purposes.

add_column(table_name, column_name, type, options = {})

Adds a new column to the named table. See TableDefinition#column for details of the options you can use.

begin_db_transaction()

Begins a transaction.

change_column(table_name, column_name, type, options = {})

Changes the column of a table.

change_column_default(table_name, column_name, default)

Changes the default value of a table column.

change_column_null(table_name, column_name, null, default = nil)

client_min_messages()

Returns the current client message level.

client_min_messages=(level)

Set the client message level.

columns(table_name, name = nil)

Returns the list of all column definitions for a table.

commit_db_transaction()

Commits a transaction.

create_database(name, options = {})

Create a new PostgreSQL database. Options include :owner, :template, :encoding, :tablespace, and :connection_limit (note that MySQL uses :charset while PostgreSQL uses :encoding).

Example:

  create_database config[:database], config
  create_database 'foo_development', :encoding => 'unicode'

create_savepoint()

current_database()

Returns the current database name.

disconnect!()

Close the connection.

encoding()

Returns the current database encoding format.

escape_bytea(value)

Escapes binary strings for bytea input to the database.

execute(sql, name = nil)

Executes an SQL statement, returning a PGresult object on success or raising a PGError exception otherwise.

indexes(table_name, name = nil)

Returns the list of all indexes for a table.

insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

Executes an INSERT query and returns the new record‘s ID

outside_transaction?()

The ruby-pg driver supports inspecting the transaction status, while the ruby-postgres driver does not.

reconnect!()

Close then reopen the connection.

release_savepoint()

remove_index(table_name, options = {})

Drops an index from a table.

rename_column(table_name, column_name, new_column_name)

Renames a column in a table.

rename_table(name, new_name)

Renames a table.

rollback_db_transaction()

Aborts a transaction.

rollback_to_savepoint()

schema_search_path()

Returns the active schema search path.

schema_search_path=(schema_csv)

Sets the schema search path to a string of comma-separated schema names. Names beginning with $ have to be quoted (e.g. $user => ’$user’). See: www.postgresql.org/docs/current/static/ddl-schemas.html

This should be not be called manually but set in database.yml.

select_rows(sql, name = nil)

Executes a SELECT query and returns an array of rows. Each row is an array of field values.

supports_ddl_transactions?()

supports_insert_with_returning?()

supports_migrations?()

Does PostgreSQL support migrations?

supports_savepoints?()

supports_standard_conforming_strings?()

Does PostgreSQL support standard conforming strings?

table_alias_length()

Returns the configured supported identifier length supported by PostgreSQL, or report the default of 63 on PostgreSQL 7.x.

tables(name = nil)

Returns the list of all tables in the schema search path or a specified schema.

type_to_sql(type, limit = nil, precision = nil, scale = nil)

Maps logical Rails types to PostgreSQL-specific data types.

unescape_bytea(value)

Unescapes bytea output from a database to the binary string it represents. NOTE: This is NOT an inverse of escape_bytea! This is only to be used

      on escaped binary output from database drive.

update_sql(sql, name = nil)

Executes an UPDATE query and returns the number of affected tuples.

postgresql_version()

Returns the version of the connected PostgreSQL version.