npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mastra/pg

v0.2.4

Published

Postgres provider for Mastra - includes both vector and db storage capabilities

Downloads

6,209

Readme

@mastra/pg

PostgreSQL implementation for Mastra, providing both vector similarity search (using pgvector) and general storage capabilities with connection pooling and transaction support.

Installation

npm install @mastra/pg

Prerequisites

  • PostgreSQL server with pgvector extension installed (if using vector store)
  • PostgreSQL 11 or higher

Usage

Vector Store

import { PgVector } from '@mastra/pg';

const vectorStore = new PgVector('postgresql://user:pass@localhost:5432/db');

// Create a new table with vector support
await vectorStore.createIndex({
  indexName: 'my_vectors',
  dimension: 1536,
  metric: 'cosine',
});

// Add vectors
const ids = await vectorStore.upsert({
  indexName: 'my_vectors',
  vectors: [[0.1, 0.2, ...], [0.3, 0.4, ...]],
  metadata: [{ text: 'doc1' }, { text: 'doc2' }],
});

// Query vectors
const results = await vectorStore.query({
  indexName: 'my_vectors',
  queryVector: [0.1, 0.2, ...],
  topK: 10, // topK
  filter: { text: 'doc1' }, // filter
  includeVector: false, // includeVector
  minScore: 0.5, // minScore
});

// Clean up
await vectorStore.disconnect();

Storage

import { PostgresStore } from '@mastra/pg';

const store = new PostgresStore({
  host: 'localhost',
  port: 5432,
  database: 'mastra',
  user: 'postgres',
  password: 'postgres',
});

// Create a thread
await store.saveThread({
  id: 'thread-123',
  resourceId: 'resource-456',
  title: 'My Thread',
  metadata: { key: 'value' },
});

// Add messages to thread
await store.saveMessages([
  {
    id: 'msg-789',
    threadId: 'thread-123',
    role: 'user',
    type: 'text',
    content: [{ type: 'text', text: 'Hello' }],
  },
]);

// Query threads and messages
const savedThread = await store.getThread('thread-123');
const messages = await store.getMessages('thread-123');

Configuration

The PostgreSQL store can be initialized with either:

  • connectionString: PostgreSQL connection string (for vector store)
  • Configuration object with host, port, database, user, and password (for storage)

Connection pool settings:

  • Maximum connections: 20
  • Idle timeout: 30 seconds
  • Connection timeout: 2 seconds

Features

Vector Store Features

  • Vector similarity search with cosine, euclidean, and dot product metrics
  • Advanced metadata filtering with MongoDB-like query syntax
  • Minimum score threshold for queries
  • Automatic UUID generation for vectors
  • Table management (create, list, describe, delete, truncate)
  • Uses pgvector's IVFFLAT indexing with 100 lists by default
  • Supports HNSW indexing with configurable parameters
  • Supports flat indexing

Storage Features

  • Thread and message storage with JSON support
  • Atomic transactions for data consistency
  • Efficient batch operations
  • Rich metadata support
  • Timestamp tracking
  • Cascading deletes

Supported Filter Operators

The following filter operators are supported for metadata queries:

  • Comparison: $eq, $ne, $gt, $gte, $lt, $lte
  • Logical: $and, $or
  • Array: $in, $nin
  • Text: $regex, $like

Example filter:

{
  $and: [{ age: { $gt: 25 } }, { tags: { $in: ['tag1', 'tag2'] } }];
}

Vector Store Methods

  • createIndex({indexName, dimension, metric?, indexConfig?, defineIndex?}): Create a new table with vector support
  • upsert({indexName, vectors, metadata?, ids?}): Add or update vectors
  • query({indexName, queryVector, topK?, filter?, includeVector?, minScore?}): Search for similar vectors
  • defineIndex({indexName, metric?, indexConfig?}): Define an index
  • listIndexes(): List all vector-enabled tables
  • describeIndex(indexName): Get table statistics
  • deleteIndex(indexName): Delete a table
  • truncateIndex(indexName): Remove all data from a table
  • disconnect(): Close all database connections

Storage Methods

  • saveThread(thread): Create or update a thread
  • getThread(threadId): Get a thread by ID
  • deleteThread(threadId): Delete a thread and its messages
  • saveMessages(messages): Save multiple messages in a transaction
  • getMessages(threadId): Get all messages for a thread
  • deleteMessages(messageIds): Delete specific messages

Related Links