
Security News
Open VSX Begins Implementing Pre-Publish Security Checks After Repeated Supply Chain Incidents
Following multiple malicious extension incidents, Open VSX outlines new safeguards designed to catch risky uploads earlier.
node-pg-migrate
Advanced tools
Node.js database migration management built exclusively for postgres. (But can also be used for other DBs conforming to SQL standard - e.g. CockroachDB.)
Started by Theo Ephraim, then handed over to Salsita Software and now maintained by @Shinigami92.
If you don't already have the pg library installed, you will need to add pg as either a direct or dev dependency
npm add pg
npm add --save-dev node-pg-migrate
Installing this module adds a runnable file into your node_modules/.bin directory. If installed globally (with the -g option), you can run node-pg-migrate and if not, you can run ./node_modules/.bin/node-pg-migrate.js
Add "migrate": "node-pg-migrate" to scripts section of your package.json so you are able to quickly run commands.
Run npm run migrate create my-first-migration. It will create file xxx_my-first-migration.js in migrations folder.
Open it and change contents to:
export const up = (pgm) => {
pgm.createTable('users', {
id: 'id',
name: { type: 'varchar(1000)', notNull: true },
createdAt: {
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
});
pgm.createTable('posts', {
id: 'id',
userId: {
type: 'integer',
notNull: true,
references: '"users"',
onDelete: 'CASCADE',
},
body: { type: 'text', notNull: true },
createdAt: {
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
});
pgm.createIndex('posts', 'userId');
};
Save migration file.
Now you should put your DB connection string to DATABASE_URL environment variable and run npm run migrate up.
(e.g. DATABASE_URL=postgres://test:test@localhost:5432/test npm run migrate up)
You should now have two tables in your DB :tada:
If you want to change your schema later, you can e.g. add lead paragraph to posts:
Run npm run migrate create posts_lead, edit xxx_posts_lead.js:
export const up = (pgm) => {
pgm.addColumns('posts', {
lead: { type: 'text', notNull: true },
});
};
Run npm run migrate up and there will be a new column in posts table :tada:
Want to know more? Read docs:
Full docs are available at https://salsita.github.io/node-pg-migrate
Why only Postgres? - By writing this migration tool specifically for postgres instead of accommodating many databases, we can actually provide a full featured tool that is much simpler to use and maintain. I was tired of using crippled database tools just in case one day we switch our database.
Async / Sync - Everything is async in node, and that's great, but a migration tool should really just be a fancy wrapper that generates SQL. Most other migration tools force you to bring in control flow libraries or wrap everything in callbacks as soon as you want to do more than a single operation in a migration. Plus by building up a stack of operations, we can automatically infer down migrations (sometimes) to save even more time.
Naming / Raw Sql - Many tools force you to use their constants to do things like specify data types. Again, this tool should be a fancy wrapper that generates SQL, so whenever possible, it should just pass through user values directly to the SQL. The hard part is remembering the syntax of the specific operation, not remembering how to type "timestamp"!
👋 Welcome, new contributors!
Whether you're a seasoned developer or just getting started, your contributions are valuable to us. Don't hesitate to jump in, explore the project, and make an impact.
FAQs
PostgreSQL database migration management tool for node.js
The npm package node-pg-migrate receives a total of 132,318 weekly downloads. As such, node-pg-migrate popularity was classified as popular.
We found that node-pg-migrate demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Following multiple malicious extension incidents, Open VSX outlines new safeguards designed to catch risky uploads earlier.

Research
/Security News
Threat actors compromised four oorzc Open VSX extensions with more than 22,000 downloads, pushing malicious versions that install a staged loader, evade Russian-locale systems, pull C2 from Solana memos, and steal macOS credentials and wallets.

Security News
Lodash 4.17.23 marks a security reset, with maintainers rebuilding governance and infrastructure to support long-term, sustainable maintenance.