Updated on 2025-03-13 GMT+08:00

psycopg2.connect()

Description

This method creates a database session and returns a new connection object.

Prototype

import os
conn=psycopg2.connect(dbname="test",user=os.getenv('user'),password=os.getenv('password'),host="127.0.0.1",port="5432")

Parameters

Table 1 psycopg2.connect parameters

Keyword

Description

dbname

Database name.

user

Username.

password

Password.

host

IP address of the database. You can specify multiple IP addresses and separate them with commas (,). By default, the UDS is used.

port

Connection port number. The default value is 5432. If the host has multiple IP addresses and the port numbers are the same, specify one port number. Otherwise, the port numbers must correspond to the IP addresses one by one and are separated by commas (,).

sslmode

SSL mode, which is used for SSL connection.

sslcert

Path of the client certificate, which is used for SSL connection.

sslkey

Path of the client key, which is used for SSL connection.

sslrootcert

Path of the root certificate, which is used for SSL connection.

hostaddr

IP address of the database.

connect_timeout

Client connection timeout interval.

client_encoding

Encoding format of the client.

application_name

Value of application_name.

fallback_application_name

Rollback value of application_name.

keepalives

Specifies whether to enable the TCP connection on the client. The value can be 1 (enabled) or 0 (disabled). The default value is 1. If the UDS connection is used, ignore this parameter.

options

Specifies the command line options sent to the server when the connection starts.

keepalives_idle

Describes inactivity before keepalive messages are sent to the server. If keepalive messages are disabled, ignore this parameter.

keepalives_interval

Specifies whether keepalive messages that are not confirmed by the server need to be resent. If keepalive messages are disabled, ignore this parameter.

keepalives_count

Specifies the number of TCP connections that may be lost before the client is disconnected from the server.

replication

Specifies that the connection uses the replication protocol instead of the common protocol.

requiressl

Supports the SSL mode.

sslcompression

Specifies the SSL compression. If this parameter is set to 1, the data sent through the SSL connection is compressed. If this parameter is set to 0, compression is disabled. If no SSL connection is established, ignore this parameter.

sslcrl

Specifies the path of the CRL, which is used to check whether the SSL server certificate is available.

requirepeer

Specifies the OS username of the server.

target_session_attrs

Specifies the type of the host to be connected. The connection is successful only when the host type is the same as the configured value. This parameter is verified only when multiple IP addresses are specified. The rules for setting target_session_attrs are as follows:

  • any: All types of hosts can be connected.
  • read-write: The connection is set up only when the connected host is readable and writable.
  • read-only: Only readable hosts can be connected.
  • primary (default value): Only the primary node in the primary/standby system can be connected.
  • standby: Only the standby node in the primary/standby system can be connected.
  • prefer-standby: The system first attempts to find a standby node for connection. If all hosts in the hosts list fail to be connected, try the any mode.

tcp_user_timeout

Specifies the maximum duration for which transmitted data can remain unacknowledged before the TCP connection is forcibly closed on an OS that supports the TCP_USER_TIMEOUT socket option. The value 0 is used by default. Ignore this parameter for UDS connections.

rw_timeout

Sets the read and write timeout interval of the client connection.

When the timeout is triggered on the libpq and the connection is closed, the running services delivered by the libpq to the database are forcibly terminated. This capability is controlled by the GUC parameter check_disconnect_query. If this parameter is set to on, the capability is supported. If this parameter is set to off, the capability is not supported.

Return Values

Connection object (for connecting to a database instance)

Examples

For details, see Examples: Common Operations.