Support for the PostgreSQL database.
For information on connecting using specific drivers, see the documentation section regarding that driver.
PostgreSQL supports sequences, and SQLAlchemy uses these as the default means of creating new primary key values for integer-based primary key columns. When creating tables, SQLAlchemy will issue the SERIAL datatype for integer-based primary key columns, which generates a sequence corresponding to the column and associated with it based on a naming convention.
To specify a specific named sequence to be used for primary key generation, use the Sequence() construct:
Table('sometable', metadata,
Column('id', Integer, Sequence('some_id_seq'), primary_key=True)
)
Currently, when SQLAlchemy issues a single insert statement, to fulfill the contract of having the “last insert identifier” available, the sequence is executed independently beforehand and the new value is retrieved, to be used in the subsequent insert. Note that when an insert() construct is executed using “executemany” semantics, the sequence is not pre-executed and normal PG SERIAL behavior is used.
PostgreSQL 8.2 supports an INSERT...RETURNING syntax which SQLAlchemy supports as well. A future release of SQLA will use this feature by default in lieu of sequence pre-execution in order to retrieve new primary key values, when available.
create_engine() accepts an isolation_level parameter which results in the command SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL <level> being invoked for every new connection. Valid values for this parameter are READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, and SERIALIZABLE.
The dialect supports PG 8.2’s INSERT..RETURNING, UPDATE..RETURNING and DELETE..RETURNING syntaxes, but must be explicitly enabled on a per-statement basis:
# INSERT..RETURNING
result = table.insert().returning(table.c.col1, table.c.col2).\
values(name='foo')
print result.fetchall()
# UPDATE..RETURNING
result = table.update().returning(table.c.col1, table.c.col2).\
where(table.c.name=='foo').values(name='bar')
print result.fetchall()
# DELETE..RETURNING
result = table.delete().returning(table.c.col1, table.c.col2).\
where(table.c.name=='foo')
print result.fetchall()
PostgreSQL supports partial indexes. To create them pass a postgresql_where option to the Index constructor:
Index('my_index', my_table.c.id, postgresql_where=tbl.c.value > 10)
Bases: sqlalchemy.types.MutableType, sqlalchemy.types.Concatenable, sqlalchemy.types.TypeEngine
Postgresql ARRAY type.
Represents values as Python lists.
Note: be sure to read the notes for MutableType regarding ORM performance implications.
Construct an ARRAY.
E.g.:
Column('myarray', ARRAY(Integer))
Arguments are:
Parameters: |
|
---|
Bases: sqlalchemy.types.TypeEngine
Bases: sqlalchemy.types.LargeBinary
Construct a LargeBinary type.
Parameter: | length – optional, a length for the column for use in DDL statements, for those BLOB types that accept a length (i.e. MySQL). It does not produce a small BINARY/VARBINARY type - use the BINARY/VARBINARY types specifically for those. May be safely omitted if no CREATE TABLE will be issued. Certain databases may require a length for use in DDL, and will raise an exception when the CREATE TABLE DDL is issued. |
---|
Bases: sqlalchemy.types.TypeEngine
Bases: sqlalchemy.types.Float
Construct a Float.
Parameters: |
|
---|
Bases: sqlalchemy.types.Enum
Construct an enum.
Keyword arguments which don’t apply to a specific backend are ignored by that backend.
Parameters: |
|
---|
Bases: sqlalchemy.types.TypeEngine
Bases: sqlalchemy.types.TypeEngine
Bases: sqlalchemy.types.TypeEngine
Bases: sqlalchemy.types.Float
Construct a Float.
Parameters: |
|
---|
Bases: sqlalchemy.types.TypeEngine
Support for the PostgreSQL database via the psycopg2 driver.
The psycopg2 driver is supported, available at http://pypi.python.org/pypi/psycopg2/ . The dialect has several behaviors which are specifically tailored towards compatibility with this module.
Note that psycopg1 is not supported.
By default, the Psycopg2 driver uses the psycopg2.extensions.UNICODE extension, such that the DBAPI receives and returns all strings as Python Unicode objects directly - SQLAlchemy passes these values through without change. Note that this setting requires that the PG client encoding be set to one which can accomodate the kind of character data being passed - typically utf-8. If the Postgresql database is configured for SQL_ASCII encoding, which is often the default for PG installations, it may be necessary for non-ascii strings to be encoded into a specific encoding before being passed to the DBAPI. If changing the database’s client encoding setting is not an option, specify use_native_unicode=False as a keyword argument to create_engine(), and take note of the encoding setting as well, which also defaults to utf-8. Note that disabling “native unicode” mode has a slight performance penalty, as SQLAlchemy now must translate unicode strings to/from an encoding such as utf-8, a task that is handled more efficiently within the Psycopg2 driver natively.
URLs are of the form postgresql+psycopg2://user:password@host:port/dbname[?key=value&key=value...].
psycopg2-specific keyword arguments which are accepted by create_engine() are:
The psycopg2 dialect fully supports SAVEPOINT and two-phase commit operations.
The psycopg2 dialect will log Postgresql NOTICE messages via the sqlalchemy.dialects.postgresql logger:
import logging
logging.getLogger('sqlalchemy.dialects.postgresql').setLevel(logging.INFO)
The following per-statement execution options are respected:
Support for the PostgreSQL database via the pg8000 driver.
URLs are of the form postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...].
pg8000 requires that the postgresql client encoding be configured in the postgresql.conf file in order to use encodings other than ascii. Set this value to the same value as the “encoding” parameter on create_engine(), usually “utf-8”.
Passing data from/to the Interval type is not supported as of yet.
Support for the PostgreSQL database via the zxjdbc JDBC connector.
The official Postgresql JDBC driver is at http://jdbc.postgresql.org/.