我应该如何使用pg-promise连接到PostGreSQL数据库?

问题描述 投票:0回答:1

因此,我已经在计算机上设置了PostGreSQL数据库,我想从我的Express服务器中进行查询。

我已经在文档中阅读了它:http://vitaly-t.github.io/pg-promise/Database.html

// Proper way to initialize and share the Database object

// Loading and initializing the library:
const pgp = require('pg-promise')({
    // Initialization Options
});

// Preparing the connection details:
const cn = 'postgres://username:password@host:port/database';

// Creating a new database instance from the connection details:
const db = pgp(cn);

// Exporting the database object for shared use:
module.exports = db;

但是我不明白我应该在这行中输入什么:

const cn = 'postgres://username:password@host:port/database';

在我的数据库中,我设置了一个角色'Lairv',这是用户名吗?关于密码,我应该用纯文本形式写密码吗?对于主机部分,我认为它是本地主机,但是我不知道端口。我找不到关于这些值是什么的文件,我不知道它应该是显而易见的还是什么,但是对我来说在代码文件中以纯文本形式编写密码似乎很奇怪。

编辑:顺便说一句,我试图只放'postgres://localhost:3000/lairv',但它不起作用,所以这就是为什么我要问这个问题

node.js postgresql express pg-promise
1个回答
0
投票

如果您在系统或服务器中安装了postgres,则端口通常为5432。也就是说,如果在安装时未更改此端口。如果您在安装主机的同一服务器上工作,则该主机为本地主机,或者您要连接的服务器的IP地址。

有了上面的一切,您就可以开始了。您可以使用任何PG-Promise方法开始将查询写入数据库。例如,db.none('INSERT INTO tablename(id,lang)VALUES($ 1,$ 2)',['2','VueJs'])。然后(... )。 catch(...)

© www.soinside.com 2019 - 2024. All rights reserved.