Updated on 2024-05-07 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 Unix domain socket 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

Determines whether to enable the TCP connection on the client. The default value is 1, indicating that the TCP connection is enabled. The value 0 indicates that the TCP connection is disabled. If the Unix domain socket 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 is disabled, ignore this parameter.

keepalives_interval

Determines whether keepalive messages that are not confirmed by the server need to be resent. If keepalive is 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

Ensures 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, the compression is disabled. If no SSL connection is established, ignore this parameter.

sslcrl

Specifies the path of the certificate revocation list (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 systems can be connected.
  • standby: Only the standby node in the primary/standby systems 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.

Return Value

Connection object (for connecting to a database instance)

Example

For details, see Examples: Common Operations.