Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Write To A Postgres Database From Node.js

NOTE : This stuff is async so keep that in mind if you do something like truncate.

TODO : show the async way to make sure stuff happens in the right order.

TODO : Put in [TODO: Code shorthand span ] where appropriate. should just go after the query but need to verify. Note that if there are other calls inside (e.g. writing back) then there might be issues. need to check that out.

This is a basic one :

const credentails = require('/Users/alans/configs/postgres_users/alans.json')
const { Pool } = require('pg')

const pool = new Pool(credentails)

pool.query(
  'INSERT INTO site_links_2021.links (file_id) VALUES ($1)',
  ['asdf'],
  (err, res) => {
    if (err) {
      throw err
    }

    console.log(res)
    // console.log('user:', res.rows[0])
  }
)

console.log('here')

Where [TODO: Code shorthand span ] looks like :

{
  "user": "alans",
  "host": "localhost",
  "database": "alans",
  "password": "blahblahblah",
  "port": 5432
}

TODO : fill this out more

https : //node - postgres.com/features/pooling

Look at this section : Single query

If you don't need a transaction or you just need to run a single query, the pool has a convenience method to run a query on any available client in the pool. This is the preferred way to query with node - postgres if you can as it removes the risk of leaking a client.