node.jspostgresqlutf-8asciicharset

Getting wrong character encoding when selecting with PostgreSQL in Node.js


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"

Solution

  • 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';