Connecting to a Database
You can run the following command to connect to the database:
EXEC SQL CONNECT TO target [AS connection-name] [USER user-name];
- dbname[@hostname][:port]
- tcp:gaussdb://hostname[:port][/dbname][?options]
- unix:gaussdb://hostname[:port][/dbname][?options]
- An SQL string containing one of the above forms
- username/password
- username SQLIDENTIFIED BY password
- username USING password
As mentioned above, the username and password parameters can be an SQL identifier, an SQL string, or a reference to a character variable.
connection_name indicates the connection name. If a program uses only one connection, you can omit it. The most recently opened connection becomes the current connection.
#include <stdlib.h> EXEC SQL CONNECT TO mydb@sql.mydomain.com; EXEC SQL CONNECT TO unix:gaussdb://sql.mydomain.com/mydb AS myconnection USER username; EXEC SQL BEGIN DECLARE SECTION; /* The values of target, user, and passwd must be read from environment variables or configuration files. Environment variables need to be configured as required. If no environment variable is used, a character string can be directly assigned. */ const char *target = getenv("EXAMPLE_TARGET_ENV"); const char *user = getenv("EXAMPLE_USERNAME_ENV"); const char *passwd = getenv("EXAMPLE_PASSWD_ENV"); EXEC SQL END DECLARE SECTION; ... EXEC SQL CONNECT TO :target USER :user USING :passwd; /* or EXEC SQL CONNECT TO :target USER :user/:passwd; */
For details about the complete usage example, see the example of using the connection syntax in CONNECT.
- In the last form, character variables are referenced. For details about how to reference C variables in SQL statements, see Host Variables.
- The format of the connection target is not described in the SQL standard. Therefore, to develop a portable application, you can use the method in the last example to encapsulate the connection target string into a variable.
- For details about the ecpg compatibility, see ecpg Compatibility.
- If ip-port is specified in the connection statement, username and password must be specified. This rule is determined by the GaussDB Kernel kernel communication authentication. If ip-port is not specified, the local $PGPORT (UDS) is used for communication.
- If the SSL protocol is used for connection, run the tcp:gaussdb://hostname[:port][/dbname][?options] command and set sslmode to disable\require in options.
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