当前位置:网站首页>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–
边栏推荐
- [opencv learning] [Canny edge detection]
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
- PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
- Research shows that "congenial" is more likely to become friends
- Explain in detail the process of realizing Chinese text classification by CNN
- 3 a VTT terminal regulator ncp51200mntxg data
- Essential for operation and maintenance - Elk log analysis system
- Unity skframework framework (XIII), question module
- 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
- [error record] cannot open "XXX" because Apple cannot check whether it contains malware
猜你喜欢
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
二、帧模式 MPLS 操作
[opencv learning] [common image convolution kernel]
C operator
Unity skframework framework (XIX), POI points of interest / information points
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
Redis database persistence
Redis数据库持久化
2、 Frame mode MPLS operation
Unity skframework Framework (XVI), package manager Development Kit Manager
随机推荐
Traverse entrylist method correctly
自主可控三维云CAD:CrownCAD赋能企业创新设计
上海交大教授:何援军——包围盒(包容体/包围盒子)
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
JS逆向之巨量创意signature签名
文件的下载与图片的预览
【云原生数据库】遇到慢SQL该怎么办(上)?
Std:: vector batch import fast de duplication method
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
[opencv learning] [common image convolution kernel]
面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
Unity skframework framework (XIII), question module
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
ADB basic commands
Everyone wants to eat a broken buffet. It's almost cold
Daily question: 1175 Prime permutation
Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
Jerry's weather direction coding table [chapter]
JS逆向之行行查data解密