4

Here I want to convert my string to unicode. I am using PostgreSQL 9.3 version. In SQL Server its much more easy:

Example:

   sql = N'select * from tabletest'; /* For nvarchar/nchar/ntext */

OR

   sql = U'select * from tabletest'; /* For varchar/char/text */

Question: How can I do the above conversion in PostgreSQL?

11
  • 1
    What is the database encoding? Try \l at the psql prompt or query show server_encoding;. Commented Jun 3, 2014 at 11:44
  • @Clodoaldo Neto, Database encoding is "UTF8". Commented Jun 3, 2014 at 11:46
  • 1
    The best and easiest is to save it as UTF-8. Any decent editor can do it. Commented Jun 3, 2014 at 11:52
  • 1
    "Unicode" is not a thing that can be converted to. You'll have to describe more clearly what you want. Commented Jun 3, 2014 at 12:04
  • 2
    @deceze I just wanted to show that as I understand his problem is just of encoding. Nothing to do with Unicode. That is, SQL Server users are mislead by wrong terminology. Commented Jun 3, 2014 at 12:20

1 Answer 1

16

PostgreSQL databases have a native character type, the "server encoding". It is usually utf-8.

All text is in this encoding. Mixed encoding text is not supported, except if stored as bytea (i.e. as opaque byte sequences).

You can't store "unicode" or "non-unicode" strings, and PostgreSQL has no concept of "varchar" vs "nvarchar". With utf-8, characters that fall in the 7-bit ASCII range (and some others) are stored as a single byte, and wider chars require more storage, so it's just automatic. utf-8 requires more storage than ucs-2 or utf-16 for text that is all "wide" characters, but less for text that's a mixture.

PostgreSQL automatically converts to/from the client's text encoding, using the client_encoding setting. There is no need to convert explicitly.

If your client is "Unicode" (which Microsoft products tend to say when they mean UCS-2 or UTF-16), then most client drivers take care of any utf-8 <--> utf-16 conversion for you.

So you should not need to care, so long as your client does I/O with correct charset options and sets a correct client_encoding that matches the data its actually sends on the wire. (This is automatic with most client drivers like PgJDBC, nPgSQL, or the Unicode psqlODBC driver).

See:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.