__init__.py 5.0 KB

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