Chef ENV settings not working - postgresql

I am attempting to set up postgresql on a Vagrant box using Chef solo and am running into some problems. I need the default postgres encoding/locale to be UTF8. By default, the locale of the precise64 Ubuntu box is set to "C" so postgres is using LATIN1 for encoding. This is what I've done so far:
I have a chef recipe that sets the locale by doing the following:
template "/etc/profile.d/lang.sh" do
source "lang.sh.erb"
mode "0644"
end
execute "locale-gen" do
command "locale-gen en_US.UTF-8"
end
execute "dpkg-reconfigure-locales" do
command "dpkg-reconfigure locales"
end
where lang.sh.erb looks like:
export LANGUAGE="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
This sets up the locale correctly, but unfortunately it doesn't modify the current environment. So I have another recipe that just sets the ENV before including postgresql
ENV["LANGUAGE"] = ENV["LANG"] = ENV["LC_ALL"] = "en_US.UTF-8"
include_recipe "postgresql::server"
This has no effect. The locale is set up correctly:
postgres#precise64:~$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
But postgres used the "C" locale when it was installed.
postgres#precise64:~$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | LATIN1 | en_US | en_US |
template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
For attribution's sake, I got all this from http://www.softr.li/blog/2012/05/22/chef-recipe-to-install-a-postgresql-server-on-a-machine-configured-with-en_us-locales.

I found that the solution that worked for me was to either in the bootstrap shell script, or as inline shell, to copy the /etc/default/lang.sh to the box prior to any recipes being run. (So should be first thing done in the Vagrant file after box definitions)
lang file:
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
From here the database should get setup with the UTF-8 encoding.
Hope this helps as I have spent days searching for solutions to this, and came up with the bits and pieces from various discussions, but realized that the problem was timing of when the values are set...

the environment variable doesn't work for chef resources.
According to the postgresql cookbook, you should set the attribute node['postgresql']['initdb_locale'] to setup the locale when initializing a database. For example, use this section under parent section name: serverin your .kitchen.yml:
attributes:
postgresql:
initdb_locale: "en_US.UTF_8"

You can drop and recreate the postgres template database as UTF-8 after the fact. Not a perfect solution, but it does work in your Chef recipe. See: http://www.pebra.net/blog/2013/06/10/when-struggling-with-postgresql-and-utf8-slash-latin/
include_recipe "postgresql::server"
include_recipe "database::postgresql"
execute "Psql template1 to UTF8" do
user "postgres"
command <<-SQL
echo "
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE' LC_CTYPE='en_US.utf8' LC_COLLATE='en_US.utf8';
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
\\c template1
VACUUM FREEZE;" | psql postgres -t
SQL
# only_if '[ $(echo "select count(*) from pg_database where datname = \'template1\' and datcollate = \'en_US.utf8\'" |psql postgres -t) -eq 0 ]'
end

Related

PostgreSQL's `initdb` fails with "invalid locale settings; check LANG and LC_* environment variables"

Already found a solution for this (see answer below), but I am not sure that it is the appropriate one; plus this may help someone else too.
Tried to set up PostgreSQL by following the documentation (18.2 Creating a Database Cluster), but got the following error on Ubuntu 18.04 (kernel: 4.15.0-22-generic):
$ initdb -D /usr/local/pgsql/data
(...)
initdb: invalid locale settings; check LANG and LC_* environment variables
Found a couple answers on Stackoverflow (1, 2) that were relevant, but these did not resolve the issue and the one on Serverfault suggested to restart the service, but PostgreSQL wasn't even running.
Tried passing the locale explicitly in every variation that I found on the system, but these failed too,
3617 2018/06/07-08:36 initdb -D ~/Downloads/ --locale=en_US.utf8
3618 2018/06/07-08:36 initdb -D ~/Downloads/ --locale=en_US.UTF8
3621 2018/06/07-08:37 initdb -D ~/Downloads/ --locale=en_US.UTF-8
3622 2018/06/07-08:37 initdb -D ~/Downloads/ --locale="en_US.UTF-8"
3623 2018/06/07-08:37 initdb -D ~/Downloads/ --locale="en_US.utf8"
3645 2018/06/07-09:24 initdb -D ~/Downloads/ --locale="en_US.utf8"
with
initdb: invalid locale name <the_option_value_above>
There was an Arch Linux forum discussion about this, but there were no solution.
2018/06/07 1214 UPDATE
I linked answers above, but perhaps wasn't explicit enough: I did look at locale -a and locale (not listing the former's output because I installed ALL of them in my attempts below):
$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
What have been tried, but did not work (and terminal has been restarted for every iteration):
https://askubuntu.com/questions/454260/how-to-solve-locale-problem
Selected and configured ALL locales.
$ sudo locale-gen en_US.UTF-8
$ sudo dpkg-reconfigure locales.
https://github.com/singularityware/singularity/issues/11
neither
echo "LC_ALL=en_US.UTF-8" >> /etc/environment
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
echo "LANG=en_US.UTF-8" > /etc/locale.conf
locale-gen en_US.UTF-8
nor
sudo apt clean
sudo apt update
sudo apt upgrade
sudo apt-get install -y locales language-pack-fi language-pack-en
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
sudo locale-gen en_US.UTF-8
sudo dpkg-reconfigure locales
https://askubuntu.com/questions/162391/how-do-i-fix-my-locale-issue/229512#229512
https://askubuntu.com/questions/114759/warning-setlocale-lc-all-cannot-change-locale
(Basically variations of the github link above, tried it anyway.)
TODO:
https://unix.stackexchange.com/questions/294845/bash-warning-setlocale-lc-all-cannot-change-locale-en-us-utf-8
From this thread:
initdb -D <your_data_location> --no-locale --encoding=UTF8
where
--locale=LOCALE set default locale for new databases
--no-locale equivalent to --locale=C
There are caveats (see warning below), but an all-utf8 database can be created using template0 (see 21.3. Template Databases).
From the client (psql):
postgres=# create database test LC_COLLATE "en_US.UTF-8" LC_CTYPE "en_US.UTF-8" template template0;
Or via createdb:
createdb --lc-collate="en_US.UTF-8" --lc-ctype="en_US.UTF-8" --template="template0" test2
Check:
$ psql
psql (10.3)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
test2 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
WARNING: This is probably not the correct solution and the workaround above is just that, a workaround.
Note the "Collate" and "Ctype" fields below in a database created with the above solution and this may cause issues, because "The results of comparisons between strings depends on LC_CTYPE. In practice, the most visible effect is the sort order." (see DBA StackExchange thread). This is also confirmed on the PostgreSQL mailing list (see this thread about this issue on a database in production). Probably the easiest way to solve this would be re-initializing/recreating the database.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
Although the question does not mention Nix, the original poster linked to this issue from the Nix discourse site so I believe this is a Nix-related issue.
I ran into this issue when running under Nix shell and found the solution here after much searching. I just had to add glibcLocales to my environment. I.e. either run nix-shell -p glibcLocales or add glibcLocales to buildInputs.
You can get a listing of the locales available in Linux with
locale -a
Use one of these.
You have to choose a locale that matches your encoding, for example
initdb -E UTF8 --locale=en_US.utf8
or
initdb -E LATIN9 --locale=et_EE.iso885915
As far as I know, you can install additional locales with
sudo apt-get install language-pack-XX
Check if the locale is enabled in /etc/locale.gen. On my fresh install of Arch Linux ARM the following line was commented out:
en_US.UTF-8 UTF-8
Run locale-gen without any arguments. It will list all the uncommented locales as it generates them.
Optional: edit /etc/locale.conf to set the system locale:
echo "LANG=en_US.UTF-8" > /etc/locale.conf
Restart the system to make all services pick up the new setting.
Run your initdb command.
for me it was an issue when I tried to upgrade postgres from 9.6 to 15.
locale should be replaced with local-provider and icu locale, e.g.:
POSTGRES_INITDB_ARGS="--locale=nl_NL --encoding=UTF8"
->
POSTGRES_INITDB_ARGS="--locale-provider=icu --icu-locale=nl_NL --encoding=UTF8

How do I change the default client_encoding in Postgres?

I'm trying to change the default value for the client_encoding configuration variable for a PostgreSQL database I'm running. I want it to be UTF8, but currently it's getting set to LATIN1.
The database is already set to use UTF8 encoding:
application_database=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+----------+----------+-------------+-------------+--------------------------------------
postgres | postgres | LATIN1 | en_US | en_US |
application_database | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres +
| | | | | application_database=Tc/postgres
template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Which according to the docs should already result in the client using UTF8 as its default client_encoding (emphasis mine):
client_encoding (string)
Sets the client-side encoding (character set). The default is to use the database encoding.
But it doesn't:
$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.
application_database=# SHOW client_encoding;
client_encoding
-----------------
LATIN1
(1 row)
I even tried using ALTER USER <user> SET ... to change the default config for the user I'm logging in as:
application_database=# ALTER USER root SET client_encoding='UTF8';
ALTER ROLE
application_database=# SELECT usename, useconfig FROM pg_shadow;
usename | useconfig
----------------------+------------------------
postgres |
root | {client_encoding=UTF8}
application_database |
(3 rows)
But that also had no effect:
$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.
application_database=# SELECT current_user;
current_user
--------------
root
(1 row)
application_database=# SHOW client_encoding;
client_encoding
-----------------
LATIN1
(1 row)
There's nothing in any of the PSQL files on my system:
vagrant#app-database:~$ cat ~/.psqlrc
cat: /home/vagrant/.psqlrc: No such file or directory
vagrant#app-database:~$ cat /etc/psqlrc
cat: /etc/psqlrc: No such file or directory
vagrant#app-database:~$ sudo su
root#app-database:/home/vagrant# cat ~/.psqlrc
cat: /root/.psqlrc: No such file or directory
I'm running PosgreSQL 9.1:
application_database=# SELECT version();
version
-------------------------------------------------------------------------------------------------------------
PostgreSQL 9.1.19 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit
(1 row)
Okay, so first things first: why isn't setting the user or database encoding having any effect?
Turns out it's because of this line from the psql documentation:
If at least one of standard input or standard output are a terminal, then psql sets the client encoding to "auto", which will detect the appropriate client encoding from the locale settings (LC_CTYPE environment variable on Unix systems). If this doesn't work out as expected, the client encoding can be overridden using the environment variable PGCLIENTENCODING.
So, in fact, your previous configuration changes actually have been working, just not in the interactive psql console. Try the following command:
sudo psql --dbname=application_database -c "SHOW client_encoding;" | cat
You should see that the client encoding is actually UTF8:
client_encoding
-----------------
UTF8
(1 row)
Now run the command again, but without piping it to cat:
sudo psql --dbname=application_database -c "SHOW client_encoding;"
You should get the result:
client_encoding
-----------------
LATIN1
(1 row)
So basically, psql is only using LATIN1 encoding for commands involving the terminal.
How can you fix this? Well, there are a few possible ways.
One would be to do as the docs suggest and set the PGCLIENTENCODING environment variable to UTF8 somewhere persistent, such as ~/.profile or ~/.bashrc to affect only your user, or /etc/environment to affect the whole system (see How to permanently set environmental variables).
Another option would be to configure your system locale settings to use en_US.utf8 instead of en_US (or equivalent). The method for doing this may vary depending on your system, but usually you can do it by modifying ~/.config/locale.conf (your user only) or /etc/default/locale or /etc/locale.conf (system-wide). This will affect more than just postgres, and I believe more closely addresses the root of the problem. You can check your current locale settings by running locale.
One other solution would be to update your psqlrc file to include SET client_encoding=UTF8;. That file is located at ~/.psqlrc (your user only) or /etc/psqlrc (system-wide). Note that this method won't affect the result of the command we were using to the client encoding earlier, since the docs state (emphasis mine):
--command=command
Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.
Did you set client_encoding in postgresql.conf (and reload config or restart)? Make sure it's UTF8 not utf8
What is the result of cat ~/.psqlrc and cat /etc/psqlrc ?
I know you're looking for server-side default, but on the client, you can set an OS envvar:
export PGCLIENTENCODING=UTF8
to do this for all users (on that machine), put that in /etc/profile

Change Database Collation, Ctype in Postgresql

how do I change Collation, cType to - en_IN from en_US.UTF-8
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
: postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
: postgres=CTc/postgres
my current postgresversion is 8.4
ive installed it using
sudo apt-get install postgresql-8.4 postgresql-contrib-8.4
im doing this in my ubuntu amazon server ec2
It's not necessary to recreate the whole database cluster. You need however to recreate your database.
Run createdb with these options (man createdb):
-E encoding, --encoding=encoding
Specifies the character encoding scheme to be used in this
database. The character sets supported by the PostgreSQL server
are described in Section 22.3.1, “Supported Character Sets”, in
the documentation.
-l locale, --locale=locale
Specifies the locale to be used in this database. This is
equivalent to specifying both --lc-collate and --lc-ctype.
--lc-collate=locale
Specifies the LC_COLLATE setting to be used in this database.
--lc-ctype=locale
Specifies the LC_CTYPE setting to be used in this database.
It seems you really can't change the collation of an existing database:
=> ALTER DATABASE dbname SET "Collate" To Russian;
ERROR: unrecognized configuration parameter "Collate"
Note that you can set collation for a table or a column, see a good tutorial on collations in PostgreSQL.
My recommendation:
take a pg_dumpall
re-initialize the db cluster, making sure the locale information is correct
restore your dump.
I have found that sometimes it is possible that one may have to create a db with template template0 (-T template0 from bash or WITH TEMPLATE template0 from psql) to use a non-init-db locale.
I had to change to POSIX.UTF-8. I managed that with the following commands:
su postgres
psql
\l
update pg_database set datcollate='POSIX.UTF-8', datctype='POSIX.UTF-8' where datname='databasename';
\l
its Very very Simple Solutions.
Step1. su - postgres
Step2. psql
Setp3. update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = 'icinga'; (dont'forget to add ;)
Step4. \l to check

PostgreSQL is being installed with SQL_ASCII using Puppet

I'm trying to build Vagrant box provisioned with Puppet.
I use Ubuntu 12.04 LTS box from http://files.vagrantup.com/precise64.box.
I also use http://forge.puppetlabs.com/puppetlabs/postgresql module to install PostgreSQL with the following manifest:
class db {
class { 'postgresql': version => '9.1' }
class { 'postgresql::server': }
}
class { 'db': }
It installs correctly, but databases are created with SQL_ASCII encoding:
$ psql -l -U postgres
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+-----------+---------+-------+-----------------------
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
After googling I've thought it might be a locale problem, but it looks correct to me:
$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Do you know what should I do to be able to install PostgreSQL with UTF8 encoding default? It'd be also nice to be able to use Puppet for this.
Apparently, it is Puppet bug http://projects.puppetlabs.com/issues/4695. Added the following code from issue comments as a workaround:
# workaround for http://projects.puppetlabs.com/issues/4695
# when PostgreSQL is installed with SQL_ASCII encoding instead of UTF8
exec { 'utf8 postgres':
command => 'pg_dropcluster --stop 9.1 main ; pg_createcluster --start --locale en_US.UTF-8 9.1 main',
unless => 'sudo -u postgres psql -t -c "\l" | grep template1 | grep -q UTF',
require => Class['postgresql::server'],
path => ['/bin', '/sbin', '/usr/bin', '/usr/sbin'],
}

encoding UTF8 does not match locale en_US; the chosen LC_CTYPE setting requires encoding LATIN1

While trying to install opennms :
/usr/share/opennms/bin/install -l /usr/local/lib -dis
I get the error:
ERROR: encoding UTF8 does not match locale en_US Detail: The chosen LC_CTYPE setting requires encoding LATIN1.
and I'm not sure how to proceed, as I've tried creating the DB several different ways (see below).
Full log:
==============================================================================
OpenNMS Installer
==============================================================================
Configures PostgreSQL tables, users, and other miscellaneous settings.
- searching for jicmp:
- trying to load /usr/local/lib/libjicmp.so: NO
- trying to load /usr/lib/jni/libjicmp.so: OK
- searching for jicmp6:
- trying to load /usr/local/lib/libjicmp6.so: NO
- trying to load /usr/lib/jni/libjicmp6.so: OK
- searching for jrrd:
- trying to load /usr/local/lib/libjrrd.so: NO
- trying to load /usr/lib/jni/libjrrd.so: NO
- trying to load /usr/lib/jni/libjrrd.so: NO
- trying to load /usr/lib/jvm/jdk1.6.0_34/jre/lib/amd64/server/libjrrd.so: NO
- trying to load /usr/lib/jvm/jdk1.6.0_34/jre/lib/amd64/libjrrd.so: NO
- trying to load /usr/lib/jvm/jdk1.6.0_34/jre/../lib/amd64/libjrrd.so: NO
- trying to load /libjrrd.so: NO
- trying to load /usr/share/opennms/lib/libjrrd.so: NO
- trying to load /usr/share/opennms/lib/linux64/libjrrd.so: NO
- trying to load /usr/java/packages/lib/amd64/libjrrd.so: NO
- trying to load /usr/lib64/libjrrd.so: NO
- trying to load /lib64/libjrrd.so: NO
- trying to load /lib/libjrrd.so: NO
- trying to load /usr/lib/libjrrd.so: NO
- trying to load /usr/lib/jni/libjrrd.so: NO
- trying to load /usr/lib/libjrrd.so: NO
- trying to load /usr/local/lib/libjrrd.so: NO
- trying to load /opt/NMSjicmp/lib/32/libjrrd.so: NO
- trying to load /opt/NMSjicmp/lib/64/libjrrd.so: NO
- trying to load /opt/NMSjicmp6/lib/32/libjrrd.so: NO
- trying to load /opt/NMSjicmp6/lib/64/libjrrd.so: NO
- Failed to load the optional jrrd library.
- This error is not fatal, since jrrd is only required for optional features.
- For more information, see http://www.opennms.org/index.php/jrrd
- using SQL directory... /usr/share/opennms/etc
- using create.sql... /usr/share/opennms/etc/create.sql
* using 'postgres' as the PostgreSQL user for OpenNMS
* using 'opennms' as the PostgreSQL database name for OpenNMS
Exception in thread "main" org.opennms.core.schema.MigrationException: an error occurred creating the OpenNMS database
at org.opennms.core.schema.Migrator.createDatabase(Migrator.java:428)
at org.opennms.core.schema.Migrator.prepareDatabase(Migrator.java:444)
at org.opennms.install.Installer.install(Installer.java:236)
at org.opennms.install.Installer.main(Installer.java:949)
Caused by: org.postgresql.util.PSQLException: ERROR: encoding UTF8 does not match locale en_US
Detail: The chosen LC_CTYPE setting requires encoding LATIN1.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:366)
at org.opennms.core.schema.Migrator.createDatabase(Migrator.java:425)
... 3 more
List of databases:
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | LATIN1 | en_US | en_US |
rhq | rhqadmin | LATIN1 | en_US | en_US |
template0 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | LATIN1 | en_US | en_US | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
I have used the following 3 initdb options but none of them work
/usr/local/pgsql/bin/initdb -E UTF-8 --pgdata=/usr/local/pgsql/data
/usr/local/pgsql/bin/initdb -E LATIN1 --pgdata=/usr/local/pgsql/data
/usr/local/pgsql/bin/initdb -E en_US.UTF8 --pgdata=/usr/local/pgsql/data
Also, do i need to delete all data in /usr/local/pgsql/data before i use initdb ?
appending locale command stdout:
$locale
LANG=en_US
LANGUAGE=en_US:
LC_CTYPE="en_US"
LC_NUMERIC="en_US"
LC_TIME="en_US"
LC_COLLATE="en_US"
LC_MONETARY="en_US"
LC_MESSAGES="en_US"
LC_PAPER="en_US"
LC_NAME="en_US"
LC_ADDRESS="en_US"
LC_TELEPHONE="en_US"
LC_MEASUREMENT="en_US"
LC_IDENTIFICATION="en_US"
LC_ALL=
I combined two solutions from other sites that did the job (this answer works for Ubuntu server 12.04 and PGSQL 9.1):
Create a file: nano /etc/profile.d/lang.sh.
Add the following:
export LANGUAGE="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
Save the file
Restart the shell or run all export commands manually in current shell instance
Reconfigure so the encoding can be UTF8 ([got it from here][1])
sudo su postgres
psql
update pg_database set datistemplate=false where datname='template1';
drop database Template1;
create database template1 with owner=postgres encoding='UTF-8'
lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;
update pg_database set datistemplate=true where datname='template1';
Use template1 for db creation.
Thanks for locale output. OpenNMS seems to be using your en_US (non-UTF-8) locale in order to create postgres db, and this is wrong. This should work:
export LANG=en_US.UTF-8
locale # confirm that it shows only en_US.UTF-8 for all settings
# finally, run your opennms installer
/usr/share/opennms/bin/install -l /usr/local/lib -dis
this worked for me:
CREATE DATABASE mydb WITH ENCODING='UTF8' LC_CTYPE='en_US.UTF-8' LC_COLLATE='en_US.UTF-8' OWNER=postgres TEMPLATE=template0 CONNECTION LIMIT=-1;
This is happening because your system is setup to use Latin1 encoding instead of UTF-8. Your language is set correctly to en_US, but the encoding is not set to UTF-8. Try running this:$
localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
This will set tell all of your applications that are installed after the change (i think) to use the English language with unicode encoding. More information can be found here: https://www.linux.com/learn/docs/ldp/790-Unicode-HOWTO#s3
I had a similar issue so I:
aptitude purge postresql ...
localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
aptitude install postresql ....
and then postregsql knew to set itself to use the english language with unicode encoding.
Of course, you could do this on a per database basis as described here: http://www.postgresql.org/docs/9.1/static/multibyte.html
I have the same error:
ERROR: new collation (en_US.utf8) is incompatible with the collation of the template database (en_US.UTF-8)
HINT: Use the same collation as in the template database, or use template0 as template.
Solution:
dpkg-reconfigure locales (choose en_US.UTF-8)
then:
sudo -u postgres psql
postgres=# update pg_database set datistemplate=false where datname='template1';
UPDATE 1
postgres=# drop database Template1;
DROP DATABASE
postgres=# create database template1 with owner=postgres encoding='UTF-8'
postgres-# lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0;
CREATE DATABASE
postgres=# update pg_database set datistemplate=true where datname='template1';
UPDATE 1
Test:
postgres=# CREATE DATABASE Person
WITH
OWNER = Person
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
CREATE DATABASE
It works :)
Change the locales and languages of SO for compatibilty with BD.
dpkg-reconfigure locales
enjoy!
I meet the similar error. old database LC_CTYPE is UTF-8, but new one is en_US.UTF-8, I solve the problem after reinit database.
initdb --pgdata=/path/to/postgresql -E utf8
You should choice the right -E parameter
I have had the "same issue" while installing odoo. Turns out that before installing odoo you have to configure locale
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales
And then configuler locale with UTF-8
BUT you have to configure locale before posgres installation.