Node-postgres: Character encoding of the database

Created on 24 Dec 2015  路  2Comments  路  Source: brianc/node-postgres

Hi,

I can't figure out how to specify character encoding of the database. My database uses iso-8859-1 instead of utf8. All the strings I get from the database are considered utf8, and contain some "strange" characters. Is there any way to specify the encoding for automatic conversion?

I use the following connection string:

"postgres://myusername@localhost/mydbname"

I tried the following with no success:

"postgres://myusername@localhost/mydbname?encoding=iso-8859-1"

Thanks!
Alex

Most helpful comment

I got a workaround: I "surround" my real query in the following SQL query:

client.query("SET client_encoding to 'latin1';" , function(err, empty_result_to_fix_encoding) {
  client.query("SELECT ...", function (err, result) { /* ... */ });  // "Real" query goes here.
});

All 2 comments

I got a workaround: I "surround" my real query in the following SQL query:

client.query("SET client_encoding to 'latin1';" , function(err, empty_result_to_fix_encoding) {
  client.query("SELECT ...", function (err, result) { /* ... */ });  // "Real" query goes here.
});

Client or Pool configuration object is not very well documented. Complete (afaik) accepted params are like these I suppose. I'm using Node.js 5.9+.

const pg = require('pg');
const Pool = require('pg-pool'); // pg.Pool was undefined for some reason
const Promise = require('bluebird'); // or some other Promise implementation
const Client = pg.Client;

config = {
  user: 'postgres',
  password: '123456',
  host: 'db.mydomain.com',
  port: 5432,
  database: 'myDb',
  client_encoding: 'utf8', // This is not documented, I discovered it via inspecting "https://github.com/iceddev/pg-connection-string"
  ssl: true,
  max: 20,
  min: 1,
  idleTimeoutMillis: 1000,
  Client: Client, // This is client constructor, can be interchanged with native client
  Promise: Promise // This is internally used promise constructor
};

var pool = new Pool(config);

// Go on with your new pool
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AhmedBHameed picture AhmedBHameed  路  3Comments

frmoded picture frmoded  路  3Comments

gpanainte picture gpanainte  路  3Comments

spollack picture spollack  路  4Comments

ossdev07 picture ossdev07  路  3Comments