__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 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. from psycopg2 import tz # noqa
  53. # Register default adapters.
  54. from psycopg2 import extensions as _ext
  55. _ext.register_adapter(tuple, _ext.SQL_IN)
  56. _ext.register_adapter(type(None), _ext.NoneAdapter)
  57. # Register the Decimal adapter here instead of in the C layer.
  58. # This way a new class is registered for each sub-interpreter.
  59. # See ticket #52
  60. from decimal import Decimal # noqa
  61. from psycopg2._psycopg import Decimal as Adapter # noqa
  62. _ext.register_adapter(Decimal, Adapter)
  63. del Decimal, Adapter
  64. def connect(dsn=None, connection_factory=None, cursor_factory=None, **kwargs):
  65. """
  66. Create a new database connection.
  67. The connection parameters can be specified as a string:
  68. conn = psycopg2.connect("dbname=test user=postgres password=secret")
  69. or using a set of keyword arguments:
  70. conn = psycopg2.connect(database="test", user="postgres", password="secret")
  71. Or as a mix of both. The basic connection parameters are:
  72. - *dbname*: the database name
  73. - *database*: the database name (only as keyword argument)
  74. - *user*: user name used to authenticate
  75. - *password*: password used to authenticate
  76. - *host*: database host address (defaults to UNIX socket if not provided)
  77. - *port*: connection port number (defaults to 5432 if not provided)
  78. Using the *connection_factory* parameter a different class or connections
  79. factory can be specified. It should be a callable object taking a dsn
  80. argument.
  81. Using the *cursor_factory* parameter, a new default cursor factory will be
  82. used by cursor().
  83. Using *async*=True an asynchronous connection will be created. *async_* is
  84. a valid alias (for Python versions where ``async`` is a keyword).
  85. Any other keyword parameter will be passed to the underlying client
  86. library: the list of supported parameters depends on the library version.
  87. """
  88. kwasync = {}
  89. if 'async' in kwargs:
  90. kwasync['async'] = kwargs.pop('async')
  91. if 'async_' in kwargs:
  92. kwasync['async_'] = kwargs.pop('async_')
  93. if dsn is None and not kwargs:
  94. raise TypeError('missing dsn and no parameters')
  95. dsn = _ext.make_dsn(dsn, **kwargs)
  96. conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  97. if cursor_factory is not None:
  98. conn.cursor_factory = cursor_factory
  99. return conn