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
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:
|
Return Value
Connection object (for connecting to a database instance)
Examples
For details, see Example: Common Operations.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot