Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

372 lines (259 sloc) 14.9 KB

Connecting to a database

All Sequel activity begins with connecting to a database, which creates a Sequel::Database object. The Database object is used to create datasets and execute queries. Sequel provides a powerful and flexible mechanism for connecting to databases. There are two main ways to establish database connections:

  1. Using the Sequel.connect method

  2. Using the specialized adapter method (Sequel.sqlite, Sequel.postgres, etc.)

The connection options needed depend on the adapter being used, though most adapters share the same basic connection options.

If you are only connecting to a single database, it is recommended that you store the database object in a constant named DB. This should never be required, but it is the convention that most Sequel code uses.

Using the Sequel.connect method

The connect method usually takes a well-formed URI, which is parsed into connection options needed to open the database connection. The scheme/protocol part of the URI is used to determine the adapter to use:

DB = Sequel.connect('postgres://user:password@localhost/blog') # Uses the postgres adapter

You can use URI query parameters to specify options:

DB = Sequel.connect('postgres://localhost/blog?user=user&password=password')

You can also pass an additional option hash with the connection string:

DB = Sequel.connect('postgres://localhost/blog' :user=>'user', :password=>'password')

You can also just use an options hash without a connection string. If you do this, you must provide the adapter to use:

DB = Sequel.connect(:adapter=>'postgres', :host=>'localhost', :database=>'blog', :user=>'user', :password=>'password')

All of the above statements are equivalent.

Using the specialized adapter method

The specialized adapter method is similar to Sequel.connect with an options hash, except that it automatically populates the :adapter option and assumes the first argument is the :database option, unless the first argument is a hash. So the following statements are equivalent to the previous statements.

DB = Sequel.postgres('blog', :host=>'localhost', :user=>'user', :password=>'password')
DB = Sequel.postgres(:host=>'localhost', :user=>'user', :password=>'password', :database=>'blog')

Passing a block to either method

Both the Sequel.connect method and the specialized adapter methods take a block. If you provide a block to the method, Sequel will create a Database object and pass it as an argument to the block. When the block returns, Sequel will disconnect the database connection. For example:

Sequel.connect('sqlite://blog.db'){|db| puts db[:users].count}

General connection options

These options are shared by all adapters unless otherwise noted.

  • :adapter - The adapter to use

  • :database - The name of the database to which to connect

  • :default_schema - The database schema to use by default.

  • :host - The hostname of the database server to which to connect

  • :logger - An array of SQL loggers to log to

  • :loggers - An array of SQL loggers to log to

  • :password - The password for the user account

  • :servers - A hash with symbol keys and hash or proc values, used with master/slave/partitioned database configurations

  • :single_threaded - Whether to use a single-threaded (non-thread safe) connection pool

  • :test - Whether to test that a valid database connection can be made (false by default)

  • :user - The user account name to use logging in

The following options can be specified and are passed to the the database's internal connection pool.

  • :after_connect - A proc called after a new connection is made, with the connection object (default: nil)

  • :max_connections - The maximum size of the connection pool (default: 4 connections on most databases)

  • :pool_sleep_time - The number of seconds to sleep before trying to acquire a connection again (default: 0.001 seconds)

  • :pool_timeout - The number of seconds to wait if a connection cannot be acquired before raising an error (default: 5 seconds)

Adapter specific connection options

The following sections explain the options and behavior specific to each adapter. If the library the adapter requires is different from the name of the adapter scheme, it is listed specifically, otherwise you can assume that is requires the library with the same name.


Requires: win32ole

The ADO adapter provides connectivity to ADO databases in Windows. It relies on WIN32OLE library, so it isn't usable on other operating systems (except possibly through WINE, but that's fairly unlikely).

The following options are supported:


Sets the time in seconds to wait while attempting to execute a command before cancelling the attempt and generating an error. Specifically, it sets the ADO CommandTimeout property. If this property is not set, the default of 30 seconds is used.


The driver to use in the ADO connection string. If not provided, a default of “SQL Server” is used.


The full ADO connection string. If this is provided, the usual options are ignored.


Sets the Provider of this ADO connection (for example, “SQLOLEDB”). If you don't specify a provider, the default one used by WIN32OLE has major problems, such as creating a new native database connection for every query, which breaks things such as transactions and temporary tables.

Pay special attention to the :provider option, as without specifying a provider, many things will be broken. The SQLNCLI10 provider appears to work well if you are connecting to Microsoft SQL Server, but it is not the default as that would break backwards compatability.

Example connections:

# SQL Server
# Access 2007
Sequel.ado(:conn_string=>'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=drive:\\path\\filename.accdb')
# Access 2000
Sequel.ado(:conn_string=>'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=drive:\\path\\filename.mdb')
# Excel 2000 (for table names, use a dollar after the sheet name, e.g. Sheet1$)
Sequel.ado(:conn_string=>'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=drive:\\path\\filename.xls;Extended Properties=Excel 8.0;')


Amalgalite is an ruby extension that provides self contained access to SQLite, so you don't need to install SQLite separately. As amalgalite is a file backed database, the :host, :user, and :password options are not used.

  • :database - The name of the database file

  • :timeout - The busy timeout period given in milliseconds

Without a database argument, assumes a memory database, so you can do:


Handles paths in the connection string similar to the SQLite adapter, so see the sqlite section below for details.


Requires: db2/db2cli

I'm not even sure exactly how this works, or if it works at all (I've never heard from anyone who attempted to use it). It uses the SQL_HANDLE_DBC constant to get a handle, and respects the :database, :user, and :password options. It doesn't appear to respect the :host or :port options.


Allows access to a multitude of databases via ruby-dbi. Additional options:

  • :db_type - Specifying 'mssql' allows Microsoft SQL Server specific syntax to be used. Otherwise has no effect.

DBI connection strings are a preprocessed a bit, and are specified with a dbi- in front of the protocol. Examples:


While the DBI adapter does work, it is recommended that you use another adapter if your database supports it.


Requires: data_objects

The DataObjects adapter supports PostgreSQL, MySQL, and SQLite. One possible advantage of using DataObjects is that it does the typecasting in C, which may be faster than the other adapters.

Similar to the JDBC adapter, the DO adapter only cares about connection strings, which can either be the String argument given to Sequel.connect directly or contained in a :uri or :url option. The DO adapter passes through the connection string directly to DataObjects, it does no processing of it.

Connection string examples:



Requires: fb (using code at

Does not support the :port option.


Does not support the :host or :port options. Depending on the configuration of your server it may be necessary to either set

DB.quote_identifier = false

or set


in the scripts environment.


Requires: java

Houses Sequel's JDBC support when running on JRuby. Support for individual database types is done using sub adapters. There are currently subadapters for PostgreSQL, MySQL, SQLite, H2, Oracle, MSSQL, JTDS, and AS400. All except Oracle, MSSQL, and AS400 can load the jdbc-* gem, for those you need to have the .jar in your CLASSPATH or load the Java class manually before calling Sequel.connect.

You just use the JDBC connection string directly, which can be specified via the string given to Sequel.connect or via the :uri, :url, or :database options. Sequel does no preprocessing of the string, it passes it directly to JDBC. So if you have problems getting a connection string to work, look up the JDBC documentation.

Note that when using a JDBC adapter, the best way to use Sequel is via Sequel.connect, NOT Sequel.jdbc. Use the JDBC connection string when connecting, which will be in a different format than the native connection string. The connection string should start with 'jdbc:'. For PostgreSQL, use 'jdbc:postgresql:', and for SQLite you do not need 2 preceding slashes for the database name (use no preceding slashes for a relative path, and one preceding slash for an absolute path).

Example connection strings:


You can also use JNDI connection strings:


The following additional options are supported:

  • :convert_types - If set to false, does not attempt to convert some Java types to ruby types. Setting to false roughly doubles performance when selecting large numbers of rows. Note that you can't provide this option inside the connection string (as that is passed directly to JDBC), you have to pass it as a separate option.


The MySQL adapter does not support the pure-ruby MySQL adapter that used to ship with ActiveRecord, it requires the native adapter.

The following additional options are supported:

  • :auto_is_null - If set to true, makes “WHERE primary_key IS NULL” select the last inserted id.

  • :charset - Same as :encoding, :encoding takes precedence.

  • :compress - Whether to compress data sent/received via the socket connection.

  • :config_default_group - The default group to read from the in the MySQL config file.

  • :config_local_infile - If provided, sets the Mysql::OPT_LOCAL_INFILE option on the connection with the given value.

  • :encoding - Specify the encoding/character set to use for the connection.

  • :socket - Can be used to specify a Unix socket file to connect to instead of a TCP host and port.

  • :timeout - Sets the wait_timeout for the connection, defaults to 1 month.

  • :read_timeout - Set the timeout in seconds for reading back results to a query.

  • :connect_timeout - Set the timeout in seconds before a connection attempt is abandoned.


The ODBC adapter allows you to connect to any database with the appropriate ODBC drivers installed. The :database option given ODBC database should be the DSN (Descriptive Service Name) from the ODBC configuration.

Sequel.odbc('mydb', :user => "user", :password => "password")

The :host and :port options are not respected. The following additional options are supported:

  • :db_type - Can be specified as 'mssql' or 'progress' to use SQL syntax specific to those databases.

  • :driver - The name of the ODBC driver to utilize.


The :port option is ignored.


Requires: oci8

The following additional options are supported:

  • :privilege - The Oracle privilege level.


Requires: pg (or postgres if pg is not available)

The Sequel postgres adapter works with the pg, postgres, and postgres-pr ruby libraries. The pg library is the best supported, as it supports real bound variables and prepared statements.

The following additional options are supported:

  • :charset - Same as :encoding, :encoding takes precedence

  • :encoding - Set the client_encoding to the given string


As SQLite is a file-based database, the :host and :port options are ignored, and the :database option should be a path to the file.


# In Memory databases:

# Relative Path

# Absolute Path

The following additional options are supported:

  • :timeout - the busy timeout to use in milliseconds (default: 5000).


swift is a ruby 1.9 only library, so you'll need to be running ruby 1.9. It can connect to SQLite, MySQL, and PostgreSQL, and you must specify which database using the db_type option.




Because the underscore is not a valid character in a URI schema, the adapter is named tinytds instead of tiny_tds. The connection options are passed directly to tiny_tds, except that the tiny_tds :username option is set to the Sequel :user option. If you want to use an entry in the freetds.conf file, you should specify the :dataserver option with that name as the value. Some other options that you may want to set are :login_timeout, :timeout, :tds_version, :azure, :appname, and :encoding, see the tiny_tds README for details.

For highest performance, you should disable any identifier output method when using the tinytds adapter, which probably means disabling any identifier input method as well. The default for Microsoft SQL Server is to :downcase identifiers on output and :upcase them on input, so the highest performance will require changing the setting from the default.

The Sequel tinytds adapter requires tiny_tds >= 0.4.5, and if you are using FreeTDS 0.91, you must at least be using 0.91rc2 (0.91rc1 does not work).

Jump to Line
Something went wrong with that request. Please try again.