@@ -1787,10 +1787,11 @@ async def connect(dsn=None, *,
1787
1787
Connection arguments specified using as a single string in the
1788
1788
`libpq connection URI format`_:
1789
1789
``postgres://user:password@host:port/database?option=value``.
1790
- The following options are recognized by asyncpg: host, port,
1791
- user, database (or dbname), password, passfile, sslmode.
1792
- Unlike libpq, asyncpg will treat unrecognized options
1793
- as `server settings`_ to be used for the connection.
1790
+ The following options are recognized by asyncpg: ``host``,
1791
+ ``port``, ``user``, ``database`` (or ``dbname``), ``password``,
1792
+ ``passfile``, ``sslmode``, ``sslcert``, ``sslkey``, ``sslrootcert``,
1793
+ and ``sslcrl``. Unlike libpq, asyncpg will treat unrecognized
1794
+ options as `server settings`_ to be used for the connection.
1794
1795
1795
1796
.. note::
1796
1797
@@ -1912,6 +1913,51 @@ async def connect(dsn=None, *,
1912
1913
1913
1914
*ssl* is ignored for Unix domain socket communication.
1914
1915
1916
+ Example of programmatic SSL context configuration that is equivalent
1917
+ to ``sslmode=verify-full&sslcert=..&sslkey=..&sslrootcert=..``:
1918
+
1919
+ .. code-block:: pycon
1920
+
1921
+ >>> import asyncpg
1922
+ >>> import asyncio
1923
+ >>> import ssl
1924
+ >>> async def main():
1925
+ ... # Load CA bundle for server certificate verification,
1926
+ ... # equivalent to sslrootcert= in DSN.
1927
+ ... sslctx = ssl.create_default_context(
1928
+ ... ssl.Purpose.SERVER_AUTH,
1929
+ ... cafile="path/to/ca_bundle.pem")
1930
+ ... # If True, equivalent to sslmode=verify-full, if False:
1931
+ ... # sslmode=verify-ca.
1932
+ ... sslctx.check_hostname = True
1933
+ ... # Load client certificate and private key for client
1934
+ ... # authentication, equivalent to sslcert= and sslkey= in
1935
+ ... # DSN.
1936
+ ... sslctx.load_cert_chain(
1937
+ ... "path/to/client.cert",
1938
+ ... keyfile="path/to/client.key",
1939
+ ... )
1940
+ ... con = await asyncpg.connect(user='postgres', ssl=sslctx)
1941
+ ... await con.close()
1942
+ >>> asyncio.run(run())
1943
+
1944
+ Example of programmatic SSL context configuration that is equivalent
1945
+ to ``sslmode=require`` (no server certificate or host verification):
1946
+
1947
+ .. code-block:: pycon
1948
+
1949
+ >>> import asyncpg
1950
+ >>> import asyncio
1951
+ >>> import ssl
1952
+ >>> async def main():
1953
+ ... sslctx = ssl.create_default_context(
1954
+ ... ssl.Purpose.SERVER_AUTH)
1955
+ ... sslctx.check_hostname = False
1956
+ ... sslctx.verify_mode = ssl.CERT_NONE
1957
+ ... con = await asyncpg.connect(user='postgres', ssl=sslctx)
1958
+ ... await con.close()
1959
+ >>> asyncio.run(run())
1960
+
1915
1961
:param dict server_settings:
1916
1962
An optional dict of server runtime parameters. Refer to
1917
1963
PostgreSQL documentation for
@@ -1970,6 +2016,10 @@ async def connect(dsn=None, *,
1970
2016
.. versionchanged:: 0.22.0
1971
2017
The *ssl* argument now defaults to ``'prefer'``.
1972
2018
2019
+ .. versionchanged:: 0.24.0
2020
+ The ``sslcert``, ``sslkey``, ``sslrootcert``, and ``sslcrl`` options
2021
+ are supported in the *dsn* argument.
2022
+
1973
2023
.. _SSLContext: https://docs.python.org/3/library/ssl.html#ssl.SSLContext
1974
2024
.. _create_default_context:
1975
2025
https://docs.python.org/3/library/ssl.html#ssl.create_default_context
0 commit comments