環境
CentOS 6.5
PostgreSQL 8.4(yum版)
psqlの実行結果の表示がずれて見難い。
test_db=> SELECT user_id, name, cnt FROM test_tbl; user_id | name | cnt ---------+--------------------+----- 145 | 伊達 正宗 | 2 253 | 織田 信長 | 2 389 | 武田 信玄 | 2
エンコーディングを確認。
test_db=> \encoding SQL_ASCII
test_db=> \encoding utf8
これでずれなくなる。
user_id | name | cnt ---------+---------------+----- 145 | 伊達 正宗 | 2 253 | 織田 信長 | 2 389 | 武田 信玄 | 2
※上記の表示がずれているのは、はてなBlogが原因っぽい。
毎回エンコードを打ち込むのが面倒なら、以下のファイルを作成すると、psql 直後に実行される。
$ cat ~/.psqlrc
\encoding utf8
また、PostgreSQLのテンプレートのエンコードがASCIIになっていた模様。
test_db=> \l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権
-------------+----------+------------------+----------+-------------------+-----------------------
postgres | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres
: postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres
: postgres=CTc/postgres
DBを作成する時に、文字エンコードをオプションで指定すれば良い。
psql => CREATE DATABASE database_name TEMPLATE template0 ENCODING 'UTF8';
または
$ createdb -E UTF8 -T template0 database_name
なお、テンプレートを指定しないと次のようなエラーが出る。
CREATE DATABASE database_name ENCODING 'UTF8'; ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII) HINT: Use the same encoding as in the template database, or use template0 as template.