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.
- Log in to your MantaHQ Dashboard.
- In the sidebar navigation, click API Keys.
- Click New API Key.
- Enter a descriptive name for your key.
- 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.
- Navigate to Connections in your dashboard.
- Click Create a new DB Connection.
- Enter a connection name.
- Select Postgres from the list of database types.
- Select your preferred connection method:
- Connection String
- Manual Configuration
Provide a standard PostgreSQL URI string to connect instantly. - Connection String: Enter your database URI (e.g.,
postgresql://user:password@host:port/database).Enter your database credentials individually. - Host: The server address (IP or domain) of your database. - Port: The port number. - Username: The database user authorized for access. - Password: The password associated with the username. - Database: The name of the specific database to connect to.
- 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.
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.
// 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
tablevalue 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
greaterThanorin) into standard SQL syntax for PostgreSQL.