使用Node.js操控pg
安裝
可使用以下模組,與資料庫連線。
npm install pg --savehttps://github.com/brianc/node-postgres
https://node-postgres.com/features/queries
可選擇使用client或使用Pool
這邊預設 Pool connection 數量預設只有 10
建議修改增大,不然一次發出許多 query 會被中斷,並且 server 無法與 DB 連線,並且沒有顯示錯誤訊息。
https://node-postgres.com/api/pool
執行指令前輸入相關連線設定
$ PGUSER=dbuser \
PGHOST=database.server.com \
PGPASSWORD=secretpassword \
PGDATABASE=mydb \
PGPORT=3211 \
node script.js或是寫在程式
Query
Insert
如果是直接寫,記得要是value單引號,不然會出現沒有該column name的錯誤
使用Pool
記得要release() 不然程式會當掉
現在可以直接
存入 timestamp
使用 Sequelize
如果出現 error: syntax error at or near 或是 column _ not exist
注意 table 名稱不要取到 SQL 保留字,例如 user 改為 users, order 要改為 orders
Last updated
Was this helpful?