How do you change the character encoding of a postgres database?

后端 未结 5 1407
我在风中等你
我在风中等你 2020-11-29 16:26

I have a database that was set up with the default character set SQL_ASCII. I want to switch it to UNICODE. Is there an easy way to do that?

5条回答
  •  忘掉有多难
    2020-11-29 16:43

    First off, Daniel's answer is the correct, safe option.

    For the specific case of changing from SQL_ASCII to something else, you can cheat and simply poke the pg_database catalogue to reassign the database encoding. This assumes you've already stored any non-ASCII characters in the expected encoding (or that you simply haven't used any non-ASCII characters).

    Then you can do:

    update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'thedb'
    

    This will not change the collation of the database, just how the encoded bytes are converted into characters (so now length('£123') will return 4 instead of 5). If the database uses 'C' collation, there should be no change to ordering for ASCII strings. You'll likely need to rebuild any indices containing non-ASCII characters though.

    Caveat emptor. Dumping and reloading provides a way to check your database content is actually in the encoding you expect, and this doesn't. And if it turns out you did have some wrongly-encoded data in the database, rescuing is going to be difficult. So if you possibly can, dump and reinitialise.

提交回复
热议问题