Connect to Live HubDB Data in PostGresSQL Interface through CData Connect AI
There are a vast number of PostgreSQL clients available on the Internet. PostgreSQL is a popular interface for data access. When you pair PostgreSQL with CData Connect AI, you gain database-like access to live HubDB data from PostgreSQL. In this article, we walk through the process of connecting to HubDB data in Connect AI and establishing a connection between Connect AI and PostgreSQL using a TDS foreign data wrapper (FDW).
CData Connect AI provides a pure SQL Server interface for HubDB, allowing you to query data from HubDB without replicating the data to a natively supported database. Using optimized data processing out of the box, CData Connect AI pushes all supported SQL operations (filters, JOINs, etc.) directly to HubDB, leveraging server-side processing to return the requested HubDB data quickly.
Connect to HubDB in Connect AI
CData Connect AI uses a straightforward, point-and-click interface to connect to data sources.
- Log into Connect AI, click Sources, and then click Add Connection
- Select "HubDB" from the Add Connection panel
-
Enter the necessary authentication properties to connect to HubDB.
There are two authentication methods available for connecting to HubDB data source: OAuth Authentication with a public HubSpot application and authentication with a Private application token.
Using a Custom OAuth App
AuthScheme must be set to "OAuth" in all OAuth flows. Be sure to review the Help documentation for the required connection properties for you specific authentication needs (desktop applications, web applications, and headless machines).
Follow the steps below to register an application and obtain the OAuth client credentials:
- Log into your HubSpot app developer account.
- Note that it must be an app developer account. Standard HubSpot accounts cannot create public apps.
- On the developer account home page, click the Apps tab.
- Click Create app.
- On the App info tab, enter and optionally modify values that are displayed to users when they connect. These values include the public application name, application logo, and a description of the application.
- On the Auth tab, supply a callback URL in the "Redirect URLs" box.
- If you're creating a desktop application, set this to a locally accessible URL like http://localhost:33333.
- If you are creating a Web application, set this to a trusted URL where you want users to be redirected to when they authorize your application.
- Click Create App. HubSpot then generates the application, along with its associated credentials.
- On the Auth tab, note the Client ID and Client secret. You will use these later to configure the driver.
Under Scopes, select any scopes you need for your application's intended functionality.
A minimum of the following scopes is required to access tables:
- hubdb
- oauth
- crm.objects.owners.read
- Click Save changes.
- Install the application into a production portal with access to the features that are required by the integration.
- Under "Install URL (OAuth)", click Copy full URL to copy the installation URL for your application.
- Navigate to the copied link in your browser. Select a standard account in which to install the application.
- Click Connect app. You can close the resulting tab.
Using a Private App
To connect using a HubSpot private application token, set the AuthScheme property to "PrivateApp."
You can generate a private application token by following the steps below:
- In your HubDB account, click the settings icon (the gear) in the main navigation bar.
- In the left sidebar menu, navigate to Integrations > Private Apps.
- Click Create private app.
- On the Basic Info tab, configure the details of your application (name, logo, and description).
- On the Scopes tab, select Read or Write for each scope you want your private application to be able to access.
- A minimum of hubdb and crm.objects.owners.read is required to access tables.
- After you are done configuring your application, click Create app in the top right.
- Review the info about your application's access token, click Continue creating, and then Show token.
- Click Copy to copy the private application token.
To connect, set PrivateAppToken to the private application token you retrieved.
- Log into your HubSpot app developer account.
- Click Save & Test
-
Navigate to the Permissions tab in the Add HubDB Connection page and update the User-based permissions.
Add a Personal Access Token
When connecting to Connect AI through the REST API, the OData API, or the Virtual SQL Server, a Personal Access Token (PAT) is used to authenticate the connection to Connect AI. It is best practice to create a separate PAT for each service to maintain granularity of access.
- Click on the Gear icon () at the top right of the Connect AI app to open the settings page.
- On the Settings page, go to the Access Tokens section and click Create PAT.
-
Give the PAT a name and click Create.
- The personal access token is only visible at creation, so be sure to copy it and store it securely for future use.
With the connection configured and a PAT generated, you are ready to connect to HubDB data from PostgreSQL.
Build the TDS Foreign Data Wrapper
The Foreign Data Wrapper can be installed as an extension to PostgreSQL, without recompiling PostgreSQL. The tds_fdw extension is used as an example (https://github.com/tds-fdw/tds_fdw).
- You can clone and build the git repository via something like the following view source:
Note: If you have several PostgreSQL versions and you do not want to build for the default one, first locate where the binary for pg_config is, take note of the full path, and then append PG_CONFIG=sudo apt-get install git git clone https://github.com/tds-fdw/tds_fdw.git cd tds_fdw make USE_PGXS=1 sudo make USE_PGXS=1 installafter USE_PGXS=1 at the make commands. - After you finish the installation, then start the server:
sudo service postgresql start - Then go inside the Postgres database
Note: Instead of localhost you can put the IP where your PostgreSQL is hosted.psql -h localhost -U postgres -d postgres
Connect to HubDB data as a PostgreSQL Database and query the data!
After you have installed the extension, follow the steps below to start executing queries to HubDB data:
- Log into your database.
- Load the extension for the database:
CREATE EXTENSION tds_fdw; - Create a server object for HubDB data:
CREATE SERVER "HubDB1" FOREIGN DATA WRAPPER tds_fdw OPTIONS (servername'tds.cdata.com', port '14333', database 'HubDB1'); - Configure user mapping with your email and Personal Access Token from your Connect AI account:
CREATE USER MAPPING for postgres SERVER "HubDB1" OPTIONS (username '[email protected]', password 'your_personal_access_token' ); - Create the local schema:
CREATE SCHEMA "HubDB1"; - Create a foreign table in your local database:
#Using a table_name definition: CREATE FOREIGN TABLE "HubDB1".NorthwindProducts ( id varchar, Name varchar) SERVER "HubDB1" OPTIONS(table_name 'HubDB.NorthwindProducts', row_estimate_method 'showplan_all'); #Or using a schema_name and table_name definition: CREATE FOREIGN TABLE "HubDB1".NorthwindProducts ( id varchar, Name varchar) SERVER "HubDB1" OPTIONS (schema_name 'HubDB', table_name 'NorthwindProducts', row_estimate_method 'showplan_all'); #Or using a query definition: CREATE FOREIGN TABLE "HubDB1".NorthwindProducts ( id varchar, Name varchar) SERVER "HubDB1" OPTIONS (query 'SELECT * FROM HubDB.NorthwindProducts', row_estimate_method 'showplan_all'); #Or setting a remote column name: CREATE FOREIGN TABLE "HubDB1".NorthwindProducts ( id varchar, col2 varchar OPTIONS (column_name 'Name')) SERVER "HubDB1" OPTIONS (schema_name 'HubDB', table_name 'NorthwindProducts', row_estimate_method 'showplan_all'); - You can now execute read/write commands to HubDB:
SELECT id, Name FROM "HubDB1".NorthwindProducts;
More Information & Free Trial
Now, you have created a simple query from live HubDB data. For more information on connecting to HubDB (and more than 200 other data sources), visit the Connect AI page. Sign up for a free trial and start working with live HubDB data in PostgreSQL.