Updated on 2023-10-23 GMT+08:00

psycopg2.connect()

Function

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

Prototype

conn=psycopg2.connect(dbname="test",user="postgres",password="secret",host="127.0.0.1",port="5432")

Parameter

Table 1 psycopg2.connect parameters

Keyword

Description

dbname

Database name

user

Username

password

Password

host

IP address of the database. The default type is UNIX socket.

port

Connection port number. The default value is 5432.

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 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 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

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, 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.

Return Value

Connection object (for connecting to the GaussDB database instance)

Examples

For details, see Example: Common Operations.