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:
- Add a SqlSourceSystem modeling the Postgres source system to your package.
- Configure the JdbcCredentials authorizing the connection to the external Postgres table.
- Add a SqlSourceCollection modeling the target Postgres table to your package.
- 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:
{
"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 hostnameport: Postgres server portdatastoreType: Specifies that the JdbcCredentials authorizes a connection to Postgresdatabase: The name of the Postgres database that you are connecting tousername: The account with authorization to access the Postgres database tablepassword: The password for this account
For example, run the following from console to configure the JdbcCredentials:
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 tablesource: Name of the External Type that models the schema of the external Postgres tablesourceSystem: 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:
{
"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:
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.
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;
}
Postgres data types are mapped to PrimitiveTypes according to the following table:
| Postgres Data Types | C3 AI Data Types |
|---|---|
| integer | int, int32 |
| smallint | int16 |
| bigint | bigint |
| bytea | binary |
| boolean | boolean |
| date, time, timestamp | datetime |
| character, character varying, smallserial, serial, text | string |
| numeric | decimal |
| real | float |
| double precision | double |
| json | json |
You can also access the source data types to validate the type inference:
SqlSourceCollection.forName("Vendors").connect().columns;
Complete the External Entity Type definition:
entity type Vendors mixes External, NoSystemCols schema name "vendors" {
@db(dataTypeOverride='string')
id: ~ schema name "vendor_id"
vendorName: string schema name "vendor_name"
}Your table must include an id field. If your table does not have a column called id, you can change the schema name for the corresponding id field with the following annotation:
@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:
c3Grid(Vendors.fetch());