Last active
November 20, 2018 17:47
-
-
Save stephencweiss/0219371bb6d8d04f14de5f243fb18c43 to your computer and use it in GitHub Desktop.
A gist showing how to connect and insert to Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```js | |
//insertToPostgres.js | |
const { client } = require('pg'); | |
const config = require('../config.json'); | |
... | |
const table = 'descriptions'; | |
const fields = 'product_id, product_name, features, tech_specs' | |
const host = config.host; | |
const user = config.username; | |
const pw = config.password; | |
const db = config.database; | |
const port = config.port; | |
const conString = `postgres://${user}:${pw}@${host}:${port}/${db}`; | |
const client = new Client({ | |
connectionString: conString, | |
}); | |
client.connect(); | |
... | |
const insertQueryText = 'INSERT INTO descriptions (product_id, product_name, features, tech_specs) VALUES ($1, $2, $3, $4) RETURNING *'; | |
const insertQueryValues = [record.productId, record.productName, record.features, record.techSpecs]; | |
client.query(insertQueryText, insertQueryValues) | |
.then(res => console.log(res)) | |
.catch(err => console.error(chalk.red(`There was an error! --> `), err)) | |
... | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment