GaussDB(DWS) allows you to use an ODBC driver to connect to the database through an ECS on the cloud platform or over the Internet.
For details about how to use the ODBC API, see the official document.
GaussDB(DWS) also supports open-source ODBC driver: PostgreSQL ODBC 09.01.0200 or later.
tar -xvf unixODBC-2.3.0.tar.gz
cd unixODBC-2.3.0 vi configure
Change the value of LIB_VERSION to the following. Save the change and exit.
LIB_VERSION="1:0:0"
./configure --enable-gui=no make make install
unzip dws_odbc_driver_for_linux.zip
vi /usr/local/etc/odbcinst.ini
Copy the following content to the file:
[DWS] Driver64=/usr/local/lib/psqlodbcw.so
The parameters are as follows:
vi /usr/local/etc/odbc.ini
Copy the following content to the configuration file, save the modification, and exit.
[DWSODBC]
Driver=DWS
Servername=10.10.0.13
Database=gaussdb
Username=dbadmin
Password=password
Port=8000
Sslmode=allow
Parameter |
Description |
Example Value |
---|---|---|
[DSN] |
Data source name. |
[DWSODBC] |
Driver |
Driver name, corresponding to DriverName in odbcinst.ini. |
Driver=DWS |
Servername |
Server IP address. |
Servername=10.10.0.13 |
Database |
Name of the database to be connected to. |
Database=gaussdb |
Username |
Database username. |
Username=dbadmin |
Password |
Database user password. |
Password=password |
Port |
Port number of the server. |
Port=8000 |
Sslmode |
SSL certification mode. This parameter is enabled for the cluster by default. Values and meanings:
NOTE:
The SSL mode delivers higher security than the common mode. By default, the SSL function is enabled in a cluster to allow SSL or non-SSL connections from the client. You are advised to use the SSL mode when using ODBC to connect to a GaussDB (DWS) cluster. |
Sslmode=allow |
You can view the values of Servername and Port on the GaussDB(DWS) management console. Log in to the GaussDB(DWS) management console and click Connection Management. In the Data Warehouse Connection String area, select the target cluster and obtain Private Network Address or Public Network Address. For details, see Obtaining the Cluster Connection Address.
vi ~/.bashrc
Add the following information to the configuration file:
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH export ODBCSYSINI=/usr/local/etc export ODBCINI=/usr/local/etc/odbc.ini
source ~/.bashrc
/usr/local/bin/isql -v DWSODBC
If the following information is displayed, the connection is successful:
1 2 3 4 5 6 7 8 9 | +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> |
You can choose to automatically or manually deploy the certificate based on your needs.
Automatic deployment:
Double-click the sslcert_env.bat file. The certificate is automatically deployed to a default location.
The sslcert_env.bat file ensures the purity of the certificate environment. When the %APPDATA%\postgresql directory exists, a message will be prompted asking you whether you want to remove related directories. If you want to remove related directories, back up files in the directory.
Currently, because GaussDB(DWS) only provides a 32-bit ODBC driver, it only supports 32-bit application development. Use the 32-bit Driver Manager when you configure the data source. (Assume the Windows system drive is drive C. If another disk drive is used, modify the path accordingly.)
Do not choose Control Panel > System and Security > Administrative Tools > Data Sources (ODBC) directly.
WOW64 is the acronym for Windows 32-bit on Windows 64-bit. C:\Windows\SysWOW64\ stores the 32-bit environment on a 64-bit system. C:\Windows\System32\ stores the environment consistent with the current operating system. For technical details, see the Windows technical documents.
You can also open Driver Manager by choosing Control Panel > System and Security > Administrative Tools > Data Sources (ODBC).
You can view the values of Server and Port on the GaussDB(DWS) management console. Log in to the GaussDB(DWS) management console and click Connections. In the Data Warehouse Connection String area, select the target cluster and obtain Private Network Address or Public Network Address. For details, see Obtaining the Cluster Connection Address.
The ODBC API does not provide the database connection retry capability. You need to implement the connection retry processing in the service code.
The sample code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | // This example shows how to obtain GaussDB(DWS) data through the ODBC driver. // DBtest.c (compile with: libodbc.so) #include <stdlib.h> #include <stdio.h> #include <sqlext.h> #ifdef WIN32 #include <windows.h> #endif SQLHENV V_OD_Env; // Handle ODBC environment SQLHSTMT V_OD_hstmt; // Handle statement SQLHDBC V_OD_hdbc; // Handle connection char typename[100]; SQLINTEGER value = 100; SQLINTEGER V_OD_erg,V_OD_buffer,V_OD_err,V_OD_id; int main(int argc,char *argv[]) { // 1. Apply for an environment handle. V_OD_erg = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env); if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) { printf("Error AllocHandle\n"); exit(0); } // 2. Set environment attributes (version information). SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); // 3. Apply for a connection handle. V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc); if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) { SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); exit(0); } // 4. Set connection attributes. SQLSetConnectAttr(V_OD_hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON, 0); // 5. Connect to a data source. You do not need to enter the username and password if you have configured them in the odbc.ini file. If you have not configured them, specify the name and password of the user who wants to connect to the database in the SQLConnect function. V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "gaussdb", SQL_NTS, (SQLCHAR*) "", SQL_NTS, (SQLCHAR*) "", SQL_NTS); if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO)) { printf("Error SQLConnect %d\n",V_OD_erg); SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); exit(0); } printf("Connected !\n"); // 6. Set statement attributes. SQLSetStmtAttr(V_OD_hstmt,SQL_ATTR_QUERY_TIMEOUT,(SQLPOINTER *)3,0); // 7. Apply for a statement handle. SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt); // 8. Executes an SQL statement directly. SQLExecDirect(V_OD_hstmt,"drop table IF EXISTS testtable",SQL_NTS); SQLExecDirect(V_OD_hstmt,"create table testtable(id int)",SQL_NTS); SQLExecDirect(V_OD_hstmt,"insert into testtable values(25)",SQL_NTS); // 9. Prepare for execution. SQLPrepare(V_OD_hstmt,"insert into testtable values(?)",SQL_NTS); // 10. Bind parameters. SQLBindParameter(V_OD_hstmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0, &value,0,NULL); // 11. Execute the ready statement. SQLExecute(V_OD_hstmt); SQLExecDirect(V_OD_hstmt,"select id from testtable",SQL_NTS); // 12. Obtain the attributes of a certain column in the result set. SQLColAttribute(V_OD_hstmt,1,SQL_DESC_TYPE,typename,100,NULL,NULL); printf("SQLColAtrribute %s\n",typename); // 13. Bind the result set. SQLBindCol(V_OD_hstmt,1,SQL_C_SLONG, (SQLPOINTER)&V_OD_buffer,150, (SQLLEN *)&V_OD_err); // 14. Collect data using SQLFetch. V_OD_erg=SQLFetch(V_OD_hstmt); // 15. Obtain and return data using SQLGetData. while(V_OD_erg != SQL_NO_DATA) { SQLGetData(V_OD_hstmt,1,SQL_C_SLONG,(SQLPOINTER)&V_OD_id,0,NULL); printf("SQLGetData ----ID = %d\n",V_OD_id); V_OD_erg=SQLFetch(V_OD_hstmt); }; printf("Done !\n");pgadmin // 16. Disconnect from the data source and release handles. SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt); SQLDisconnect(V_OD_hdbc); SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc); SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env); return(0); } |