こまメモ

Twitter以上技術ブログ未満

作成済みのPostgreSQLデータベースのエンコーディングを変えたい

EncodingをSQL_ASCIIからUTF8に変えたい

postgres=# \l
                                                      List of databases
            Name             |  Owner   | Encoding  | Collate | Ctype | ICU Locale | Locale Provider |   Access privileges
-----------------------------+----------+-----------+---------+-------+------------+-----------------+-----------------------
 old_db | postgres | SQL_ASCII | C       | C     |            | libc            |
 postgres                    | postgres | SQL_ASCII | C       | C     |            | libc            |
 template0                   | postgres | SQL_ASCII | C       | C     |            | libc            | =c/postgres          +
                             |          |           |         |       |            |                 | postgres=CTc/postgres
 template1                   | postgres | SQL_ASCII | C       | C     |            | libc            | =c/postgres          +
                             |          |           |         |       |            |                 | postgres=CTc/postgres

手順

pg_dump -h X.X.X.X -p XXXX -U xxxx  old_db > db.bak
CREATE DATABASE new_db WITH ENCODING 'UTF8' TEMPLATE template0;
psql -h X.X.X.X -p XXXX -U xxxx new_db < db.bak
DROP DATABASE old_db;
ALTER DATABASE new_db RENAME to old_db;