__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """A Python driver for PostgreSQL
  2. psycopg is a PostgreSQL_ database adapter for the Python_ programming
  3. language. This is version 2, a complete rewrite of the original code to
  4. provide new-style classes for connection and cursor objects and other sweet
  5. candies. Like the original, psycopg 2 was written with the aim of being very
  6. small and fast, and stable as a rock.
  7. Homepage: https://psycopg.org/
  8. .. _PostgreSQL: https://www.postgresql.org/
  9. .. _Python: https://www.python.org/
  10. :Groups:
  11. * `Connections creation`: connect
  12. * `Value objects constructors`: Binary, Date, DateFromTicks, Time,
  13. TimeFromTicks, Timestamp, TimestampFromTicks
  14. """
  15. # psycopg/__init__.py - initialization of the psycopg module
  16. #
  17. # Copyright (C) 2003-2019 Federico Di Gregorio <fog@debian.org>
  18. # Copyright (C) 2020-2021 The Psycopg Team
  19. #
  20. # psycopg2 is free software: you can redistribute it and/or modify it
  21. # under the terms of the GNU Lesser General Public License as published
  22. # by the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # In addition, as a special exception, the copyright holders give
  26. # permission to link this program with the OpenSSL library (or with
  27. # modified versions of OpenSSL that use the same license as OpenSSL),
  28. # and distribute linked combinations including the two.
  29. #
  30. # You must obey the GNU Lesser General Public License in all respects for
  31. # all of the code used other than OpenSSL.
  32. #
  33. # psycopg2 is distributed in the hope that it will be useful, but WITHOUT
  34. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  36. # License for more details.
  37. # Import modules needed by _psycopg to allow tools like py2exe to do
  38. # their work without bothering about the module dependencies.
  39. # Note: the first internal import should be _psycopg, otherwise the real cause
  40. # of a failed loading of the C module may get hidden, see
  41. # https://archives.postgresql.org/psycopg/2011-02/msg00044.php
  42. # Import the DBAPI-2.0 stuff into top-level module.
  43. from psycopg2._psycopg import ( # noqa
  44. BINARY, NUMBER, STRING, DATETIME, ROWID,
  45. Binary, Date, Time, Timestamp,
  46. DateFromTicks, TimeFromTicks, TimestampFromTicks,
  47. Error, Warning, DataError, DatabaseError, ProgrammingError, IntegrityError,
  48. InterfaceError, InternalError, NotSupportedError, OperationalError,
  49. _connect, apilevel, threadsafety, paramstyle,
  50. __version__, __libpq_version__,
  51. )
  52. # Register default adapters.
  53. from psycopg2 import extensions as _ext
  54. _ext.register_adapter(tuple, _ext.SQL_IN)
  55. _ext.register_adapter(type(None), _ext.NoneAdapter)
  56. # Register the Decimal adapter here instead of in the C layer.
  57. # This way a new class is registered for each sub-interpreter.
  58. # See ticket #52
  59. from decimal import Decimal # noqa
  60. from psycopg2._psycopg import Decimal as Adapter # noqa
  61. _ext.register_adapter(Decimal, Adapter)
  62. del Decimal, Adapter
  63. def connect(dsn=None, connection_factory=None, cursor_factory=None, **kwargs):
  64. """
  65. Create a new database connection.
  66. The connection parameters can be specified as a string:
  67. conn = psycopg2.connect("dbname=test user=postgres password=secret")
  68. or using a set of keyword arguments:
  69. conn = psycopg2.connect(database="test", user="postgres", password="secret")
  70. Or as a mix of both. The basic connection parameters are:
  71. - *dbname*: the database name
  72. - *database*: the database name (only as keyword argument)
  73. - *user*: user name used to authenticate
  74. - *password*: password used to authenticate
  75. - *host*: database host address (defaults to UNIX socket if not provided)
  76. - *port*: connection port number (defaults to 5432 if not provided)
  77. Using the *connection_factory* parameter a different class or connections
  78. factory can be specified. It should be a callable object taking a dsn
  79. argument.
  80. Using the *cursor_factory* parameter, a new default cursor factory will be
  81. used by cursor().
  82. Using *async*=True an asynchronous connection will be created. *async_* is
  83. a valid alias (for Python versions where ``async`` is a keyword).
  84. Any other keyword parameter will be passed to the underlying client
  85. library: the list of supported parameters depends on the library version.
  86. """
  87. kwasync = {}
  88. if 'async' in kwargs:
  89. kwasync['async'] = kwargs.pop('async')
  90. if 'async_' in kwargs:
  91. kwasync['async_'] = kwargs.pop('async_')
  92. dsn = _ext.make_dsn(dsn, **kwargs)
  93. conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  94. if cursor_factory is not None:
  95. conn.cursor_factory = cursor_factory
  96. return conn