The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Write To A Postgres Database From Node.js

TODO: Pull subtitle into page object

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 `pool.end()` 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:

code_start_default_section code_end_default_section

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.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Write To A Postgres Database From Node.js

-- p

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

-- p

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

-- p

TODO: Put in `pool.end()` 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.

-- p

This is a basic one:

-- code/

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')

-- /code

-- p

Where `/Users/alans/configs/postgres_users/alans.json` looks like:

-- code/

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

-- /code

-- p

TODO: fill this out more

-- p

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

-- p

Look at this section: Single query

-- p

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.

-- p

-- hr


-- categories
-- Miscellaneous

-- metadata
-- date: 2021-11-25 23:41:49
-- id: 21rr30z8
-- status: scratch
-- type: post
-- SCRUBBED_NEO: false
-- site: aws