Skip to content

Postgres SQL

The Postgres Connector can be utilized to connect to any Postgres Database using the TCP socket. UNIX sockets are currently not supported.

Parameter Definition
Host The hostname or IP Address of the Server
Port The TCP port where the PSQL can be connected to.
Database The database within the PSQL instance to connect to.
User The username for the connection.
Password The password for the connection.
SSL Require: Encrypted but certificate is not checked
Verify Full: Encrypted and the certificate is validated
Disable: Not encrypted. (Not recommended)

Use this as a guide when configuring tools on the MCP Express User Interface.

This defines the actual SQL command to execute when the tool is called. You can utilize Placeholders using parameters. Example:

select id, status, total from orders
where customer_id={{customer_id}}
order by created_at desc
limit {{limit}};

Tips:

  • Values should be sent as parameters and never direcltyplaced into the SQL
  • Use Placeholders for values only; not for table or column names.
  • Use '%' || {{q}} || '%' for this special case to use the parameter q as a pattern for use with like or ilike statements.

import { z } from "zod";
import { defineTool, postgres, sql } from "@mcpexpress/core";
export const recentOrders = defineTool({
// Other declarations
...
// "shop_database" is the connector's name on the Connectors page; `shop` is yours.
connectors: { shop: postgres("shop_database") },
// Tool function with connectors provided during runtime
execute: async ({ customerId }, { connectors }) => {
const { rows } = await connectors.shop.query(
sql`select id, status, total from orders where customer_id = ${customerId} order by created_at desc limit 10`,
);
return { orders: rows };
},
});

This method can be used to query the database. Args:

  • text (string): The query to execute. Placeholders can be marked using $n eg. select * from orders where customer=$1
  • values (list): An ordered list of values which will be used to fill in the placeholders within the text.
  • options : The list of options

This is an alternate method to query the database. Refer query(text, values?, options?) for further details. This is simply a convenience method to enable easier formatting of the sql query (see Code Sample).