Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

psycopg2.connect()

Updated on 2024-05-07 GMT+08:00

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.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback