当前位置:网站首页>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–
边栏推荐
- Variable, "+" sign, data type
- Structured data, semi-structured data and unstructured data
- The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
- Oracle from entry to mastery (4th Edition)
- Redis database persistence
- Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
- Ali on three sides, it's really difficult to successfully get the offer rated P7
- Mysql常用命令详细大全
- Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
- Traverse entrylist method correctly
猜你喜欢

Unity skframework framework (XXI), texture filter map resource filtering tool

面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六

Unity SKFramework框架(十五)、Singleton 单例

Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)

Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具

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

屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪

Unity SKFramework框架(十二)、Score 计分模块

Unity skframework framework (XV), singleton singleton

能自动更新的万能周报模板,有手就会用!
随机推荐
Finally, someone explained the supervised learning clearly
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
Post order traversal sequence of 24 binary search tree of sword finger offer
面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
Variable, "+" sign, data type
Jerry's watch reads the alarm clock [chapter]
[opencv learning] [image filtering]
The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
Embedded software development
Hundreds of web page special effects can be used. Don't you come and have a look?
上海交大教授:何援军——包围盒(包容体/包围盒子)
[opencv learning] [common image convolution kernel]
Mobile layout (flow layout)
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
[OpenGL] notes 29. Advanced lighting (specular highlights)
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
Three methods of finding LCA of the nearest common ancestor
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
Sensor adxl335bcpz-rl7 3-axis accelerometer complies with rohs/weee