C3 AI Documentation Home

Connect Application to Postgres Database

The C3 Agentic AI Platform has a built-in Java Database Connectivity (JDBC) connector for integrating with Postgres databases.

To connect to Postgres from your application:

  1. Add a SqlSourceSystem modeling the Postgres source system to your package.
  2. Configure the JdbcCredentials authorizing the connection to the external Postgres database.
  3. Add a SqlSourceCollection modeling the target Postgres table to your package.
  4. Create an External Type modeling the schema of the external Postgres table.

For more information on the Postgres JDBC driver, see the Postgres documentation.

Model the source system

Create a SqlSourceSystem and set the name field as the identifier for the external database system.

For example, add the following PostgresSourceSystem.json to the \metadata\SqlSourceSystem directory of your package:

JSON
{
    "name": "PostgresSourceSystem"
}

Configure the credential used to authorize the JDBC connection

Create a JdbcCredentials Type instance to configure the connection to the external Postgres database, passing the following fields to the JdbcCredentials.fromServerEndpoint() method:

  • serverEndpoint: Postgres server hostname
  • port: Postgres server port
  • datastoreType: The type of datastore, DatastoreType.POSTGRES for a Postgres connection
  • database: The name of the Postgres database that you are connecting to
  • schemaName: The schema to use, or null to use the default schema
  • username: The account with authorization to access the Postgres database table
  • password: The password for this account

For example, run the following code in the console to configure the JdbcCredentials:

JavaScript
var creds = JdbcCredentials.fromServerEndpoint("HOST",
                                                PORT,
                                                DatastoreType.POSTGRES,
                                                "SOME_DB",
                                                null,
                                                "some_username",
                                                "some_password");

SqlSourceSystem.forName("PostgresSourceSystem").setCredentials(creds);

Model the table containing the data

To model the external Postgres table in your application, create a SqlSourceCollection and set the following fields:

  • name: Identifier for the Postgres table
  • source: Name of the External Type that models the schema of the external Postgres table
  • sourceSystem: Name of the Postgres SqlSourceSystem

For example, to model a table called vendors, add the following PostgresTable.json to the \metadata\SqlSourceCollection directory of your package:

JSON
{
    "name" : "Vendors",
    "source" : "Vendors",
    "sourceSystem" : {
      "type" : "SqlSourceSystem",
      "name" : "PostgresSourceSystem"
    }
}

Model the table schema

To model the schema of the Postgres table in your application, create an External entity Type. Give the entity Type a schema name that matches the name of the Postgres table exactly. The schema name is case sensitive.

Start by adding the following Vendors.c3typ file to the \src directory of your package:

Type
entity type Vendors mixes External, NoSystemCols schema name 'vendors'

Use the inferSourceType() method to see the C3 AI data types that the C3 Agentic AI Platform infers from the source data types of the table:

JavaScript
var schema = SqlSourceCollection.forName("Vendors").inferSourceType().declaredFieldTypes;

var myObject = {};
for (let i = 0; i < schema.length; i++) {
        schemaName = schema[i].schemaName;
        myObject[schemaName] = schema[i].valueType.name;
    }

Inferred C3 data types for the vendor_id and vendor_name columns

The platform maps Postgres data types to PrimitiveTypes according to the following table:

Postgres Data TypesC3 AI Data Types
integerint, int32
smallintint16
bigintbigint
byteabinary
booleanboolean
date, time, timestampdatetime
character, character varying, smallserial, serial, textstring
numericdecimal
realfloat
double precisiondouble
jsonjson

To validate the type inference, view the source data types:

JavaScript
SqlSourceCollection.forName("Vendors").connect().columns;

Source columns of the vendors table with their Postgres data types

Complete the External entity Type definition:

Type
entity type Vendors mixes External, NoSystemCols schema name "vendors" {
  
  @db(dataTypeOverride='string')
  id: ~ schema name "vendor_id"
  
  vendorName: string schema name "vendor_name"
}
Type
@db(dataTypeOverride="ID_FIELD_DATA_TYPE")
id: ~ schema name "ID_FIELD"

The example Vendors Type uses the vendor_id column as the id field.

Read data from the table

After you complete the External entity Type definition, validate the configuration. Fetch the External Type data from the Postgres table:

JavaScript
c3Grid(Vendors.fetch());

Vendors records fetched from Postgres

See also

Was this page helpful?