Postgres complaining about encoding when trying to create new database - postgresql

I installed postgres on my VM using Vagrant and Chef and when I try to create the database I keep getting this error. Not sure where to look. The user 'my_user' was created without trouble.
CREATE DATABASE db_mydb OWNER my_user ENCODING 'UTF8' TEMPLATE template1;
createdb: database creation failed: ERROR: encoding UTF8 does not match locale en_US
DETAIL: The chosen LC_CTYPE setting requires encoding LATIN1.
The VM was initially set to en_US, and I updated it to en_US.utf8

When you create the postgresql server, the default encoding is taken from your locale. If you changed the locale to UTF8 later, the server will still be initialised to latin1.
If you don't have any data yet, it's probably best to remove and recreate the server. I'm not sure exactly how to do this on your setup, but basically it should just be a case of shutting it down, deleting the database files and using initdb to recreate them. Or uninstall/reinstall the package.
If you don't want to rebuild the server, you can create the database using TEMPLATE template0 which allows you to specify a different encoding to the server default.

Related

Postgres - required databases

I have accidentally deleted the default "postgres" database from my postgres. I've read that:
Most Postgres servers have three databases defined by default: template0 , template1 and postgres . template0 and template1 are skeleton databases that are or can be used by the CREATE DATABASE command. postgres is the default database you will connect to before you have created any other databases.
I have now created again a postgres database by running CREATE DATABASE postgres.
Do I need to do anything else to basically redo deleting the "postgres" database? Or the current one is basically the same?
Thanks
The database postgres is in no way special. You should use the bootstrap superuser (normally postgres) as the database owner, then the database will be just as good as the original postgres database.
The only difference is that the new database will have an OID ≥ 16384, which identifies it as an object created after cluster initialization. However, a quick look through the source code makes me believe that we don't use that anywhere.

Azure PostgreSQL Server Service Collation Create Error

I'm trying to import my current existing database to postgre instance that is running on Azure PostgreSQL Server
I already configured my azure postgresql server parameters to use encoding as UTF8 (I'm not sure that it's applying without restart but even if i don't have option to restart it)
I'm trying to do this action with this command:
sudo -u postgres pg_dump --encoding="UTF-8" --no-owner DBNAME | psql --host=xxx.postgres.database.azure.com --port=5432 --username=xxx#yyy --dbname=DBNAME
However it's getting an error something like this:
ERROR: could not create locale "en_US.utf8": No error
I dive in the process and try to run it with UTF8 (Instead of UTF-8) and other options but it's got same error everytime.
I created a dump file to check the contents and the line that is generating this error is:
CREATE COLLATION "en_US.utf8" (lc_collate = 'en_US.utf8', lc_ctype = 'en_US.utf8');
This is causing that error. I tried to execute it manually on Azure postgre instance and i got same error too.
By the way I already created that DATABASE from console by using this SQL:
create database "DBNAME" with owner=xxx encoding='UTF-8' template template0;
Also i tried other options to create and import but non of them work.
Can someone help me?
I resolved my issue with this command:
sudo -u postgres pg_dump -Fp —no-owner DBNAME |sed “/COLLATE/s/en_US.utf8/English_United States.1252/ig”|sed "/CREATE COLLATION/s/en_US.utf8/English_United States.1252/ig"|psql --host=aaa.postgres.database.azure.com --port=5432 --username=xxx#yyy --dbname=DBNAME
The issue is coming from the server O.S. differences. My current PostgreSQL server is using Ubuntu but Azure PostgreSQL Server is using windows so the collate names are little bit different.
We've previously run into this same issue running Atlassian's Confluence.
The application would not start due to an incorrect collation type.
We were able to resolve by assigning English_United States.1252 directly.
CREATE DATABASE db
WITH ENCODING 'UTF8'
LC_COLLATE 'English_United States.1252'
LC_CTYPE 'English_United States.1252'
TEMPLATE template0
OWNER conf;
CREATE COLLATION creates a operating system independent name that can be used to refer to OS locales (in queries etc).
Here lc_collate = 'en_US.utf8' and lc_ctype = 'en_US.utf8' refer to Linux operating system locales, which are named differently on Windows, which Azure PostgreSQL uses (and they're different on MacOS, too).
On Windows, this should work
CREATE COLLATION "en_US.utf8" (lc_collate = 'English_United States', lc_ctype = 'English_United States');
And on MacOS,
CREATE COLLATION "en_US.utf8" (lc_collate = 'en_US', lc_ctype = 'en_US');
But most of the time, you don't want to create your own collations with CREATE COLLATION, but can just use the pre-included collations in PostgreSQL. And normally a dump created with pg_dump does not include any CREATE COLLATION statements, since you haven't created any yourself.
Unless you're doing something special with collations, is it possible to remove those schema-specific collations, so they won't get included in the dump?

Connecting sqlite database to firebird

Below command used for connecting database through Firebird SQL.
CONNECT "C:\Users\vkaja\Desktop\testing_mysql\newdb.db"
In newdb.db file Schema, data are dumped from SQLite.
Here newdb.db has read-write permission. But error projected here is not a valid username and password.
Statement failed, SQLCODE = -902
Firebird doesn't care about the extension of the database file. fdb is 'standard', and gdb is historical, but it could be anything. However the database you are connecting to must be a Firebird database. You can't just open a database file from a different database system (eg SQLite).
Your problem is one of authentication: you are trying to authenticate without a username + password combination, and if you haven't set the appropriate environment variables, it means Firebird tries to authenticate with an empty user and password, which doesn't exist for your Firebird install. In general you also get this error if you use a username and password that is not known to Firebird.
But even if you fix the authentication problem, you would immediately get a different error: invalid database (or similar), because the file is not a Firebird database.

Default databases in PostgreSQL

What are the default databases in PostgreSQL at the time of installation?
template1: the "default" database, which is copied when you perform "create database foo"
template0: the "minimal default" database, which serves essentially the same purpose, but typically used to create databases when restoring dumps which might already have the extra objects that are in template1, or to create a database that uses a different character encoding to the server default (template0 should only contain ASCII characters in strings)
postgres: an "administrative" database, which clients can assume exists to connect to merely to list which databases are available etc. Also, for example, pgAdmin will install the pg_agent schema in this database.
Apparently there is a database "postgres" that is created by default on each postgresql server installation.
It appears that it does not really have a well-defined purpose. According to the docs:
Creating a database cluster consists of creating the directories in
which the database data will live, generating the shared catalog
tables (tables that belong to the whole cluster rather than to any
particular database), and creating the template1 and postgres
databases. When you later create a new database, everything in the
template1 database is copied. (Therefore, anything installed in
template1 is automatically copied into each database created later.)
The postgres database is a default database meant for use by users,
utilities and third party applications.
(Source: http://www.postgresql.org/docs/8.2/static/app-initdb.html )

Locale that defaults to LATIN1 Encoding

I'm trying to create a new database cluster in postgresql that defaults to LATIN1 encoding, or at least supports it. Does anybody know what locale I could use? I am on Windows 7 64bit
Thanks
I've figured it out with help from a friend. I can use:
English_Sweden.28591
If you want to change the default encoding, you have to create a new template1 database. This database serves as template for creating new databases. Drop the current one and create a new one using template0 and use the correct encoding, latin1 in your case.
UPDATE pg_database
SET datistemplate = false -- otherwise you can't drop this database
WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH
TEMPLATE template0
ENCODING LATIN1;
Check all settings for template1 before you drop this database, maybe you want these in your new template1 as well.