Evgenii Legotckoi
Sept. 16, 2016, 11:38 a.m.

Correction database encoding of PostgreSQL from LATIN1 to UTF8

In the process of server settings for the site, I had to face some problems. In particular, the problem of the PostgreSQL database encoding. The fact is that when you install the PostgreSQL, database templates created with the coding LATIN1, and on the Django site works, using UTF8 encoding. As a result, dropped out following error when trying to insert the data:

  1. ERROR: encoding UTF8 does not match locale en_US Detail: The chosen LC_CTYPE setting requires encoding LATIN1.

Search for information, it was possible to find several solutions, including a solution that allows you to re-create the database template with the encoding UTF8. But Walk carefully on the symptoms of the problem.


Check encoding

To check the encoding used on the server and the database, you must execute the following commands.

Go to work mode with PostgreSQL:

  1. sudo -u postgres psql

psql - is a utility for working with databases and postgres - is a super user of PostgreSQL.

And execute the following commands:

  1. postgres=# SHOW SERVER_ENCODING;
  2. server_encoding
  3. -----------------
  4. LATIN1
  5. (1 row)
  6.  
  7. postgres=# \l
  8. List of databases
  9. Name | Owner | Encoding | Collate | Ctype | Access privileges
  10. -----------+----------+----------+---------+-------+-----------------------
  11. postgres | postgres | LATIN1 | en_US | en_US |
  12. template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
  13. | | | | | postgres=CTc/postgres
  14. template1 | postgres | LATIN1 | en_US | en_US | =c/postgres +
  15. | | | | | postgres=CTc/postgres
  16. (3 rows)
  17.  

The output will be shown LATIN1 encoding, both for server and data bases.

Correction for encoding template1

Now correct encoding template1, which is used to create a database.

  1. postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
  2. postgres=# DROP DATABASE Template1;
  3. postgres=# CREATE DATABASE template1 WITH owner=postgres ENCODING = 'UTF-8' lc_collate = 'en_US.utf8' lc_ctype = 'en_US.utf8' template template0;
  4. postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

In this case, we first point out that template1 is not a template for the databases. We delete this template. Then create a new template with the encoding UTF8 and set up this database as a template for new databases. Then the new database will be created with the encoding UTF8.

And check out what we have to be encoded template1 template, the server itself, by the way, will continue to be encoded LATIN1

  1. postgres=# \l
  2. List of databases
  3. Name | Owner | Encoding | Collate | Ctype | Access privileges
  4. -----------+----------+----------+------------+------------+-----------------------
  5. postgres | postgres | LATIN1 | en_US | en_US |
  6. template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
  7. | | | | | postgres=CTc/postgres
  8. template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
  9. (3 rows)

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html