Ruby-on-rails PG::Error: ERROR: 新编码 (UTF8) 不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16736891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:02:43  来源:igfitidea点击:

PG::Error: ERROR: new encoding (UTF8) is incompatible

ruby-on-railsrubypostgresql

提问by tokhi

I have installed postgresql-9.2.4from the source, now in rails app when I execute:

我已经postgresql-9.2.4从源代码安装,现在在执行时在 rails 应用程序中:

rake db:createcommand I get:

rake db:create我得到的命令:

$ bin/rake db:create RAILS_ENV="test"
PG::Error: 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.
: CREATE DATABASE "verticals_test" ENCODING = 'unicode'
/home/vagrant/my-project/.gems/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `exec
....
bin/rake:16:in `load'
bin/rake:16:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_db", "host"=>"localhost", "pool"=>5, "username"=>"my_user", "password"=>"my_password"}

any idea?

任何的想法?

回答by tokhi

Ok, below steps resolved the problem:

好的,以下步骤解决了问题:

  1. First, we need to drop template1. Templates can't be dropped, so we first modify it so t's an ordinary database:

    UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';

  2. Now we can drop it:

    DROP DATABASE template1;

  3. Now its time to create database from template0, with a new default encoding:

    CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';

  4. Now modify template1 so it's actually a template:

    UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

  5. Now switch to template1 and VACUUM FREEZE the template:

    \c template1

    VACUUM FREEZE;

  1. 首先,我们需要删除template1。模板不能删除,所以我们先修改一下,变成普通的数据库:

    UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';

  2. 现在我们可以删除它:

    DROP DATABASE template1;

  3. 现在是时候从 template0 创建数据库,使用新的默认编码:

    CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';

  4. 现在修改 template1 使其实际上是一个模板:

    UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

  5. 现在切换到 template1 和 VACUUM FREEZE 模板:

    \c template1

    VACUUM FREEZE;

Problem should be resolved.

问题应该得到解决。

回答by mihai

Make sure you have the correct setup in your database.ymlfile. You should use template0, as the error suggests:

确保您的database.yml文件中有正确的设置。您应该使用template0,正如错误所暗示的那样:

test:
  adapter: postgresql
  encoding: unicode
  database: your_db_name
  template: template0
  host: localhost
  ...

回答by lee

If you use Debian, when you install the postgresql package it will use your default locale to create the template1 database. If you have not configured your OS to use UTF-8 as a default locale, you will encounter this error.

如果您使用 Debian,当您安装 postgresql 软件包时,它将使用您的默认语言环境来创建 template1 数据库。如果您尚未将操作系统配置为使用 UTF-8 作为默认语言环境,您将遇到此错误。

In addition to the above solutions, if you are on a new installation and have no active databases, you can remove the postgresql package and set your default locale to UTF-8. The advantage of this method is you can omit locale information when creating databases in the future.

除了上述解决方案,如果您是新安装并且没有活动数据库,您可以删除 postgresql 包并将您的默认语言环境设置为 UTF-8。这种方法的优点是您可以在以后创建数据库时省略区域设置信息。

dpkg-reconfigure locales

dpkg-reconfigure locales

If you don't see the desired locale, install the locales-all package

如果您没有看到所需的语言环境,请安装 locales-all 包

apt-get install locales-all

apt-get install locales-all

Then remove postgresql

然后删除postgresql

apt-get remove --purge postgresql-<version>

apt-get remove --purge postgresql-<version>

Then reinstall or better yet upgrade to a recent release that isn't in Debian stable.

然后重新安装或更好地升级到不在 Debian stable 中的最新版本

回答by Yakob Ubaidi

As for me, I just change the line from database.yml:

至于我,我只是将行更改为database.yml

encoding: unicode

encoding: unicode

to:

到:

encoding: SQL_ASCII

encoding: SQL_ASCII

just that and it all works.

就这样,一切正常。

回答by littleforest

If your postgres installation is new and you haven't populated any databases yet, then you can remove your datadirectory and re-run the initdb command with the flag to create databases using UTF-8.

如果您的 postgres 安装是新安装的并且您还没有填充任何数据库,那么您可以删除您的data目录并重新运行带有标志的 initdb 命令以使用 UTF-8 创建数据库。

Modify this command to match your postgres installation. The -Eflag tells what character encoding should be the default. Other character encodings are listed here.

修改此命令以匹配您的 postgres 安装。该-E标志告诉什么字符编码应该是默认的。此处列出其他字符编码。

/usr/local/pgsql/bin/initdb -E UTF8 -D /usr/local/pgsql/data -U postgres

It should error and tell you that the datadirectory is not empty. Follow the instructions and remove the directory, then re-run the command. (Or, remove the datadirectory before you start, but it's always nice to see the instructions for yourself.)

它应该出错并告诉您该data目录不为空。按照说明删除目录,然后重新运行命令。(或者,data在开始之前删除该目录,但自己查看说明总是很好的。)

回答by Hussain Niazi

I had a similar issue. My database.yml was like this:-

我有一个类似的问题。我的 database.yml 是这样的:-

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: chatapp_development

test:
  <<: *default
  database: chatapp_test

production:
  <<: *default
  database: chatapp_production
  username: chatapp
  password: <%= ENV['CHATAPP_DATABASE_PASSWORD'] %>

I added template: template0 to the default settings

我添加了模板:template0 到默认设置

default: &default
  adapter: postgresql
  template: template0
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

And it worked

它起作用了