C3 AI Documentation Home

Connect Application to Postgres Database

The C3 Agentic AI Platform has a built-in 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 table.
  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.

The following sections include detailed instructions for configuring the connection. For more information, 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, you can 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 table, passing the following fields to the JdbcCredentials.fromServerEndpoint() method:

  • serverEndpoint: Postgres server hostname
  • port: Postgres server port
  • datastoreType: Specifies that the JdbcCredentials authorizes a connection to Postgres
  • database: The name of the Postgres database that you are connecting to
  • username: The account with authorization to access the Postgres database table
  • password: The password for this account

For example, run the following from console to configure the JdbcCredentials:

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

JdbcStore.forName("PostgresSourceSystem").setCredentials(creds, ConfigOverride.APP);
JdbcStore.forName("PostgresSourceSystem").setExternal(ConfigOverride.APP);

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, you can 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 with a schema name that matches the name of the table in the Postgres exactly, as it 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'

You can use the inferSourceType() method to access the C3 AI data types of the table, which the C3 Agentic AI Platform infers from the source data types.

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;
    }

Output

Postgres data types are mapped 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

You can also access the source data types to validate the type inference:

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

Output

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"

In the example Vendor Type, the vendor_id column is used as the id field.

Read data from the table

After completing the External Entity Type definition, you can validate the configuration by fetching the External Type data from the Postgres table:

JavaScript
c3Grid(Vendors.fetch());

Output

See also

Was this page helpful?