当前位置:网站首页>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–
边栏推荐
- To bypass obregistercallbacks, you need to drive the signature method
- 【云原生数据库】遇到慢SQL该怎么办(上)?
- 记忆函数的性能优化
- Principle analysis of security rememberme
- Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
- Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
- Jerry's weather direction coding table [chapter]
- How to modify the error of easydss on demand service sharing time?
- 上海交大教授:何援军——包围盒(包容体/包围盒子)
- Unity skframework framework (XIX), POI points of interest / information points
猜你喜欢

Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)

Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)

完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。

日本赌国运:Web3.0 ,反正也不是第一次失败了!

研究表明“气味相投”更易成为朋友
![[OpenGL] notes 29. Advanced lighting (specular highlights)](/img/6e/56bc7237f691a4355f0b7627b3003e.png)
[OpenGL] notes 29. Advanced lighting (specular highlights)

OpenApi-Generator:简化RESTful API开发流程

Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

Unity SKFramework框架(十三)、Question 问题模块
随机推荐
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Essential for operation and maintenance - Elk log analysis system
三翼鸟两周年:羽翼渐丰,腾飞指日可待
numpy数组计算
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
C operator
Answer: can the audio be set to on by default during easydss video on demand?
Unity skframework framework (XX), VFX lab special effects library
嵌入式软件开发
MAC (MacOS Monterey 12.2 M1) personal use PHP development
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
TVOC, VOC, VOCs gas detection + Solution
De4000h storage installation configuration
Jerry's watch time synchronization [chapter]
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
Principle analysis of security rememberme
How to get the operating system running PHP- How to get the OS on which PHP is running?
2、 Frame mode MPLS operation
Obtain file copyright information
Daily question: 1175 Prime permutation