Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including sql-pg with all npm packages installed. Try it out:

require("debug/package.json"); // debug is a peer dependency. require("pg/package.json"); // pg is a peer dependency. var sqlPg = require("sql-pg")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sql-pg v9.1.0

Complex queries can be written with normal SQL, including the values needs to be bound and prefixed with the `sql` tag.

Build Status Coverage Status Greenkeeper badge

Latest Release: v9.1.0, Latest Major Release: v9.0.0, All Releases


Complex queries can be written with normal SQL, including the values needs to be bound and prefixed with the sql tag.

SQL Make the name variable? SQL
Or even more simple? SQL


  • Built on top of pg as database driver
  • Simple data manipulation and selection methods without the need to write SQL
  • SQL Tag and Tag Helpers to write queries looks like native SQL and be secure by design
  • Write easy unit testable queries
  • Possibility to add own Tag Helpers to extend the functionality


npm install --save pg sql-pg


;(async () => {
  const { Client } = require('pg')

  const client = new Client({ connectionString: process.env.DATABASE_URL })
  await client.connect()

  const sql = require('sql-pg')({ client })


Manipulation Methods

Simple data manipulation can be done without writing any SQL Statements.

E.g. some user data manipulation:

const id = await sql.insert(
  { name: 'Sharaal', email: '', passwordhash: '...' }

await sql.update('users', { validated: 1 }, { id })

await sql.delete('users', { id })

More complex data manipulation can be done with the SQL Tag.

Selection Methods

Often needed convenient methods to check and extract query results are available with the Selection Methods.

E.g. select all not validated users:

const users = await sql.any('users', ['name', 'email'], { validated: 0 })

Also the Selection Methods supports SQL Tag as parameter for more complex selections. Because they are highly inspired by pg-promise, there are the Selection Methods any/manyOrNone, many, oneOrNone and one available.

SQL Tag and Tag Helpers

If it becomes more complex the SQL Tag and Tag Helpers are the way to go.

They are as near as possible to native SQL queries to be readable and easy to write. All variables can be directly used and will be exchanged via placeholders and given to the database separately as values. For non native values like lists, for table/column names and conditions there are Tag Helpers.

E.g. list of not activated users filtered by name:

const name = 'raa'

const users = await sql.any(
    SELECT "name", "email" FROM "users"
        "validated" = 0
        "name" LIKE ${`%${name}%`}

There are a lot more Tag Helpers available like .identifier, .table, .column(s), .value(s), .valuesList, .assignments, .conditions, .limit, .offset, .pagination, .if, .jsonColumnObject and .jsonColumnText.


Available Tag Helpers, Nested Queries, Transaction, Writing Tag Helpers, Migrations, Syntax Highlighting in Atom... All additional documentation can be found in the Wiki.


Found a bug or missing a feature? -> Create a new Issue

Found a security issue? -> Look at the Security Policy

Having questions, want to give feedback or talk to me? -> E-Mail me


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free