在node.js中选择PostgreSQL时,可以找到错误的字符编码

发布于 2025-01-29 03:32:43 字数 618 浏览 3 评论 0原文

我正在尝试使用node.js从PostgreSQL获取一些数据,但是当我使用SELECT *从表中选择 *时,它会返回我错误的字符,但是在控制台中,它没有发生。

仅显示ASCII字符,但不显示UTF-8字符。我的整个数据库都是使用UTF-8编码的,我正在尝试更改node.js中的charset。

我尝试了:

await pgClient.query(`SET client_encoding = 'utf-8'`);

但是没有起作用。

这是bash中正在返回的内容的一个示例:

SELECT * FROM example; -- This returns "María"

但这是Node.js的响应:

await pgClient.query('SELECT * FROM example'); // This returns me "Mar¢a"

I'm trying to get some data from PostgreSQL using Node.js, but when I use SELECT * FROM table it returns me wrong characters, but in console it didn't happen.

Only ASCII characters are shown, but not UTF-8 characters. My entire database is encoded using UTF-8, and I'm trying to change the charset in Node.js.

I tried this:

await pgClient.query(`SET client_encoding = 'utf-8'`);

But didn't work.

This is an example of what is returning in bash:

SELECT * FROM example; -- This returns "María"

But this is the response with Node.js:

await pgClient.query('SELECT * FROM example'); // This returns me "Mar¢a"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2025-02-05 03:32:43

问题只是一个窗口时刻。 Windows控制台可与Winxxxx编码一起使用,而Node.js中的PostgreSQL默认使用UTF8。

解决方案很简单,只需将您的client_encoding更改为utf8latin1时,请使用终端:

SET client_encoding = 'UTF8';

The problem was just a Windows moment. The Windows Console works with a WINXXXX encoding, while PostgreSQL in Node.js works with UTF8 by default.

The solution is simple, just change your client_encoding to UTF8 or LATIN1 while using the terminal:

SET client_encoding = 'UTF8';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文