Psycopg Package
- Prepare related drivers and dependent libraries. Obtain the package GaussDB-Kernel_Database version number_OS version number_64bit_Python.tar.gz from the release package.
After the decompression, the following folders are generated:
- psycopg2: psycopg2 library file
- lib: lib library file
- Load the driver.
- Before using the driver, perform the following operations:
- Decompress the driver package of the corresponding version.
tar zxvf xxxx-Python.tar.gz
- Copy psycopg2 to the site-packages folder in the Python installation directory as the root user.
su root cp psycopg2 $(python3 -c 'import site; print(site.getsitepackages()[0])') -r
- Change the psycopg2 directory permission to 755.
chmod 755 $(python3 -c 'import site; print(site.getsitepackages()[0])')/psycopg2 -R
- Add the psycopg2 directory to the environment variable $PYTHONPATH and validate it.
export PYTHONPATH=$(python3 -c 'import site; print(site.getsitepackages()[0])'):$PYTHONPATH
- For non-database users, configure the lib directory in LD_LIBRARY_PATH after decompression.
export LD_LIBRARY_PATH=path/to/lib:$LD_LIBRARY_PATH
- Decompress the driver package of the corresponding version.
- Load a database driver before creating a database connection.
1
import psycopg2
- Before using the driver, perform the following operations:
- Connect to a database.
Connect to the database in non-SSL mode.
- Use the psycopg2.connect function to obtain the connection object.
- Use the connection object to create a cursor object.
Connect to the database in SSL mode.
When you use psycopy2 to connect to the GaussDB server, you can enable SSL to encrypt the communication between the client and server. To enable SSL, you must have the server certificate, client certificate, and private key files. For details on how to obtain these files, see related documents and commands of OpenSSL.
- Use the .ini file (the configparser package of Python can parse this type of configuration file) to save the configuration information about the database connection.
- Add SSL connection parameters sslmode, sslcert, sslkey, and sslrootcert to the connection options.
- sslmode: For details about the options, see Table 1.
- sslcert: client certificate path.
- sslkey: client key path.
- sslrootcert: root certificate path.
- Use the psycopg2.connect function to obtain the connection object.
- Use the connection object to create a cursor object.
To use SSL to connect to the database, ensure that the Python interpreter is compiled in the mode of generating a dynamic link library (.so) file. You can perform the following steps to check the connection mode of the Python interpreter:
- Run the import ssl command in the Python interpreter to import SSL.
- Run the ps ux command to query the PID of the Python interpreter. Assume that the PID is ******.
- In the shell CLI, run the pmap -p ****** | grep ssl command and check whether the command output contains the path related to libssl.so. If the command output contains the path, the Python interpreter is compiled in dynamic link mode.
Table 1 sslmode options sslmode
Enable SSL Encryption
Description
disable
No
SSL connection is not enabled.
allow
Possible
If the database server requires SSL connection, SSL connection can be enabled. However, authenticity of the database server will not be verified.
prefer
Possible
If the database supports SSL connection, SSL connection is preferred. However, authenticity of the database server will not be verified.
require
Yes
SSL connection is required, but only data is encrypted. However, authenticity of the database server will not be verified.
verify-ca
Yes
SSL connection is required, and the validity of the server CA must be verified.
verify-full
Yes
The SSL connection must be enabled, which is not supported by GaussDB currently.
- Run SQL statements.
- Construct an operation statement and use %s as a placeholder. During execution, psycopg2 will replace the placeholder with the parameter value. You can add the RETURNING clause to obtain the automatically generated column values.
- Use the cursor.execute method to execute one row of SQL statement, and use the cursor.executemany method to execute multiple rows of SQL statements.
- Process the result set.
- cursor.fetchone(): fetches the next row in a query result set and returns a sequence. If no data is available, null is returned.
- cursor.fetchall(): fetches all remaining rows in a query result and returns a list. An empty list is returned when no rows are available.
For database-specific data types, such as tinyint, the corresponding columns in the query result are character strings.
- Disable the connection.
After you complete required data operations in a database, close the database connection. Call the close method such as connection.close() to close the connection.
This method closes the database connection and does not automatically call commit(). If you just close the database connection without calling commit() first, changes will be lost.
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