agentleFS
3akat-system3akat-system/nexus-starter/.claude/skills/postgresql/SKILL.md

postgresql

Skill0 starsChanged 6 months agoMITView on GitHub

PostgreSQL database patterns. TRIGGER when: SQL query, migration, psql, PostgreSQL, pg, database schema, CREATE TABLE, SELECT JOIN, INDEX, backup, pg_dump, БД запрос, DB query, миграция базы данных, database migration, таблица пользователей, users table, транзакция, transaction, база данных, database. Stack: Node.js pg/postgres driver on Beget VPS.

  • Reads credentials

21 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use redis, mongodb and 1 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • Sorcha-PlatformSkill

    redis

    Implements Redis caching, session management, and distributed coordination for the Sorcha platform. Use when: Adding caching layers, token revocation, rate limiting, SignalR backplane, or distributed state.

    Also in 3 projects that use this

  • hoodiniSkill

    mongodb

    Work with MongoDB databases using best practices. Use when designing schemas, writing queries, building aggregation pipelines, or optimizing performance. Triggers on MongoDB, Mongoose, NoSQL, aggregation pipeline, document database, MongoDB Atlas.

    Also in 3 projects that use this

  • wrsmith108Skill

    docker

    Container-based development for isolated, reproducible environments. Use when running npm commands, installing packages, executing code, or managing project dependencies. Trigger phrases include "npm install", "run the build", "start the server", "install package", or any code execution request.

    Also in 3 projects that use this

The file

Files
SKILL.md

PostgreSQL — Patterns and Commands

Connection (Node.js)

const { Pool } = require('pg');
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  // or
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  port: 5432,
});

// Query
const { rows } = await pool.query('SELECT * FROM users WHERE id = $1', [id]);

Common Patterns

Upsert (INSERT or UPDATE)

INSERT INTO settings (key, value)
VALUES ('token', 'xxx')
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value;

Pagination

SELECT * FROM orders
ORDER BY created_at DESC
LIMIT $1 OFFSET $2;
-- OFFSET = (page - 1) * limit

JSON Field

-- Save
UPDATE users SET meta = $1::jsonb WHERE id = $2;
-- Read a field
SELECT meta->>'name' FROM users WHERE id = $1;

Transaction

const client = await pool.connect();
try {
  await client.query('BEGIN');
  await client.query('UPDATE ...', [...]);
  await client.query('INSERT INTO ...', [...]);
  await client.query('COMMIT');
} catch (e) {
  await client.query('ROLLBACK');
  throw e;
} finally {
  client.release();
}

Migrations (without ORM)

// migrations/001_add_column.sql
ALTER TABLE users ADD COLUMN IF NOT EXISTS phone VARCHAR(20);
CREATE INDEX IF NOT EXISTS idx_users_phone ON users(phone);
psql -U user -d dbname -f migrations/001_add_column.sql

psql Commands

psql -U fuji -d fujidb      # connect
\dt                                    # list tables
\d tablename                           # table structure
\q                                     # exit

# Quick query without entering psql
psql -U fuji -d fujidb -c "SELECT COUNT(*) FROM users;"

Backup and Restore

# Backup
pg_dump -U fuji fujidb > backup_$(date +%Y%m%d).sql

# Restore
psql -U fuji_user fuji_routes < backup.sql

Indexes — When to Add

-- On fields used in WHERE and JOIN conditions
CREATE INDEX idx_orders_user_id ON orders(user_id);
CREATE INDEX idx_orders_status ON orders(status) WHERE status != 'done';

-- Check slow queries
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123;

Errors and Solutions

ErrorCauseSolution
ECONNREFUSEDDB not runningsystemctl start postgresql
password auth failedWrong password in .envCheck DB_PASS
relation does not existTable not createdApply migration
duplicate key valueUniqueness violationUse ON CONFLICT
too many connectionsConnection leakpool.release() in finally

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.