当前位置:网站首页>Node.js通过ODBC访问PostgreSQL数据库
Node.js通过ODBC访问PostgreSQL数据库
2022-07-02 09:59:00 【PegasusYu】
Node.js通过ODBC访问PostgreSQL数据库
Node.js除了通过pg模块访问PostgreSQL数据库,还可以通过odbc模块访问PostgreSQL数据库。这里介绍Windows操作系统下的实现。
安装PostgreSQL的ODBC支持
首先要安装PostgreSQL的ODBC支持,才能在ODBC配置工具里进行数据库引入配置。
PostgreSQL的ODBC支持可以从官方进行下载,下载地址为:https://odbc.postgresql.org
在下载页面进入msi的目录
找到最新的进行下载
这里X86后缀的是给32位系统所用的版本,X64后缀的是给64位系统所用的版本,没有标识的则是两种版本集成。
下载压缩包后,直接进行安装即可:
设置ODBC引入PostgreSQL数据库
需要将已有的PostgreSQL数据库在ODBC工具里进行配置,首先在控制面板的管理工具里找到ODBC数据源管理工具:
具体选择32位还是64位数据源,要基于PostgreSQL安装的版本是32位还是64位,PostgreSQL的Windows版本情况如下:
这里选择64位的ODBC数据源工具打开,并进行PostgreSQL数据库源的添加:
然后在弹出的配置窗口里进行参数配置:
其中:
DataSource是ODBC数据库之后的引用名;
Description自由填写;
Database是数据库名,也就是PostgreSQL管理工具pgAdmin里如下位置的某一个数据库名
SSL Mode是安全模式选择,这里disable即可;
Server是填写数据库所在的主机的IP地址,本地可以用127.0.0.1;
Port是填写数据库所在的主机的PostgreSQL服务访问端口,PostgreSQL安装时默认的端口是5432;
User Name是数据库访问的用户名;
Password是数据库访问的用户名对应的密码;
在配置完成后,可以点击Test进行连接测试,成功后点击Save按钮,即可完成设置。如下所示:
关闭ODBC数据源配置工具,后面进行Node.js odbc模块的安装。
Node.js odbc模块安装
安装好node.js后,在新建或选择的目录通过命令行进行odbc模块的安装:
npm install odbc
或
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install odbc
访问数据库
在工程目录下建立js文件,如这里的demo.js,用于读取一个数据表dbtest的内容以及在一个数据表DBDEMO插入一条记录:
demo.js:
const odbc = require('odbc');
async function myQuery() {
const connectionConfig = {
connectionString: 'DSN=PostgreSQL35W',
connectionTimeout: 10,
loginTimeout: 10,
}
const connection = await odbc.connect(connectionConfig);
const result = await connection.query('SELECT * FROM dbtest');
console.log(JSON.stringify(result));
console.log(JSON.stringify(result));
const result = await connection.query(`INSERT INTO "DBDEMO" ("INDEX", "CODE", "AUTH") VALUES('20220005', 'Insert', 'Test')`);
console.log(JSON.stringify(result));
console.log(JSON.stringify(result));
}
myQuery();
在命令行执行代码:
node demo
结果如下:
红圈部分是dbtest数据表查询出来的内容
而DBDEMO数据表也插入了一条记录
这样就实现了node.js通过odbc模块和连接访问PostgreSQL数据库。
注意事项
在使用Node.js访问PostgreSQL数据库时,需要留意数据库名及字段名大小写问题和双引号单引号包含顺序问题。具体分析及解决方案参见: Node.js访问PostgreSQL的注意事项及解决方案,对于Node.js采用odbc模块或pg模块访问PostgreSQL数据库时的问题和解决方案是一样的。
–End–
边栏推荐
- 阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
- (7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
- 免费SSL证书知多少?免费SSL证书和收费SSL证书的区别
- Apply lnk306gn-tl converter, non isolated power supply
- SSL证书的分类有哪些?如何选择合适的SSL证书?
- Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
- 【云原生数据库】遇到慢SQL该怎么办(上)?
- What are eNB, EPC and PGW?
- Unity SKFramework框架(十二)、Score 计分模块
- 每日一题:1175.质数排列
猜你喜欢

Five best software architecture patterns that architects must understand

De4000h storage installation configuration

JS iterator generator asynchronous code processing promise+ generator - > await/async

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse

Web基础
![Jerry's watch ringtone audition [article]](/img/18/905c4b64443f4efca55188e36f4b28.jpg)
Jerry's watch ringtone audition [article]

(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software

国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)

难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸

二、帧模式 MPLS 操作
随机推荐
Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
[200 opencv routines] 100 Adaptive local noise reduction filter
net share
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
OpenApi-Generator:简化RESTful API开发流程
EasyDSS点播服务分享时间出错如何修改?
de4000h存储安装配置
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
阿里发布的Redis开发文档,涵盖了所有的redis操作
[opencv learning] [image filtering]
Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
互联网常见34个术语解释
Unity skframework framework (XX), VFX lab special effects library
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
Unity skframework framework (XIII), question module
numpy数组计算
To bypass obregistercallbacks, you need to drive the signature method
PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
【OpenGL】笔记二十九、高级光照(镜面高光)