Skip to main content

Connecting a Postgres Database

You can use the MantaHQ SDK with your existing data infrastructure. By connecting an external PostgreSQL database, you can manage your records using Manta's friendly syntax while maintaining full control over your data residency.

1. Generate an API Key

To authorize communication between the SDK and your project, you must create a dedicated API key.

  1. Log in to your MantaHQ Dashboard.
  2. In the sidebar navigation, click API Keys.
  3. Click New API Key.
  4. Enter a descriptive name for your key.
  5. Copy and store the generated key immediately.

2. Configure the Connection

When you add a PostgreSQL database in Manta Studio, you assign it a DB Connection name (for example, production_db). This name is the unique identifier you will pass to the db parameter in your code.

  1. Navigate to Connections in your dashboard.
  2. Click Create a new DB Connection.
  3. Enter a connection name.
  4. Select Postgres from the list of database types.
  5. Select your preferred connection method:

    Provide a standard PostgreSQL URI string to connect instantly. - Connection String: Enter your database URI (e.g., postgresql://user:password@host:port/database).

  6. Click Connect to verify the credentials and save the configuration.

3. Initialize the Client

Initialize the SDK by providing the API Key generated in Step 1.

TypeScript
import { MantaClient } from "mantahq-sdk";

const manta = new MantaClient({
apiKey: "manta_sk_live_xxxx", // Replace with any of your API key
});

To route operations to your external database, you must include the db parameter in your request object. If this parameter is omitted, the SDK defaults to Manta's internal data tables.

TypeScript
// Use the 'db' parameter to target your external Postgres instance
await manta.fetchAllRecords({
db: "production_db", // The Connection Name assigned in Step 2
table: "users",
where: { status: "active" },
});

Understanding the snippet

  • db** parameter**: Instructs MantaHQ to execute the query against your specified PostgreSQL connection instead of Manta's internal storage (data tables).
  • Table mapping: The table value must match the name of the table exactly as it is defined in your PostgreSQL schema.
  • Schema translation: The SDK automatically translates Manta's friendly operators (such as greaterThan or in) into standard SQL syntax for PostgreSQL.