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.
Related
In order to change the collation of PostgreSQL's database template, I dropped template1 and recreated it with the 'correct' collation. I therefore got my inspiration from this question.
All fine now, but now that newly database template template1 is listed in the available databases in the tree view.
I compared database pg_database for two servers (one listing the database template1, one not), but the values of the parameters for database template1 are the same.
I would like to hide this database from in the tree view.
Anyone who can figure this out?
EDIT: this one did not bring me any further
(PostgreSQL 9.6, pgAdmin 1.22)
PgAdmin uses the following condition to show the database in the tree or not:
/* Condition used to show database */
if (settings->GetShowSystemObjects() || !database->GetSystemObject())
(...)
/* Function called above */
bool pgDatabase::GetSystemObject() const
{
if (server)
{
if (this->GetName() == wxT("template0")) return true;
return (this->GetOid() <= server->GetLastSystemOID());
}
else
{
return false;
}
}
Unless you've marked "Show System Objects in the treeview" option, I guess that your template1's oid is greater than LastSystemOID (pg_database.datlastsysoid). In this case you have three options:
Rebuild your cluster with right collation;
Accept that;
or, assuming you weren't in production, play with
pg_database.datlastsysoid and wait for side effects.
PgAdmin had an issue where template1 was shown even if the option was turned off to show system objects. This happened when you migrated your database before via pg_upgrade.
The image below shows the template database in the current pgAdmin version with the Show system object option enabled:
The issue was in the condition shown by Michel Milezzi. It is not correct to determine that a database is a template by oid <= lastSystemOid. Because when you use pg_upgrade, the oid of the template1 database is changed and the new value is higher than the lastSystemOid (i don't know if this is also a bug).
oid datname datlastsysoid datacl
13394 template0 13394 {=c/postgres,postgres=CTc/postgres}
1 template1 13394 {=c/postgres,postgres=CTc/postgres}
after using pg_upgrade:
13756 template0 13756 {=c/postgres,postgres=CTc/postgres}
16446 template1 13756 {postgres=CTc/postgres,=c/postgres}
^
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?
I did not know that template0 and template1 database templates are required in order to create empty databases. I deleted them in order to clear up postgres. Now I'm not able to create any new database. Gives me this error :
ERROR: template database "template1" does not exist
What can I do to get things going once again. I'll be very thankful for any help.
Luckily I had postgres database preserved because it was required for the postgres user to log into psql. Thus, created a template0 and template1 database :
create database template0 TEMPLATE postgres;
and same for template1. Then executed :
update pg_database set datistemplate=true where datname='template0';
for both databases to stop myself from accidentally deleting these templates again.
Everything works fine now :)
On my CentOS 7 box, I was not so lucky as to still have a database to connect to. However:
su postgres -c "initdb /var/lib/pgsql/data"
(as root) created a template0 and template 1 to work with.
For postgres 12.3 the following worked for me.
I deleted template1 not knowing what it was. I was able to create it from template0. You could also create it from postgres.
createdb -T template0 template1
createdb -T postgres template1
This is suggested in the docs -
template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects.
https://www.postgresql.org/docs/current/manage-ag-templatedbs.html
on my debian machine, i upgraded some clusters to version 11.
the template[01] databases on the target cluster have been dropped, rendering me with the ones from the older versions.
as root, i created a new cluster with pg_createcluster.
as user postgres:
dump the databases of the new cluster with pg_dumpall (with the --clean flag).
drop the new cluster.
import the resulted source in your cluster with psql.
i would remove the drop role postgres and creation of the same.
best regards,
alex
For the following MySQL create database statement, what would be the equivalent in postgresql?:
CREATE DATABASE IF NOT EXISTS `scratch`
DEFAULT CHARACTER SET = utf8
DEFAULT COLLATE = utf8_unicode_ci;
I currently have:
CREATE DATABASE "scratch"
WITH OWNER "postgres"
ENCODING 'UTF8'
TABLESPACE "pg_default";
Is that enough or should I be more specific including LOCALE as well?
Yes, you can be more specific.
For example:
CREATE DATABASE "scratch"
WITH OWNER "postgres"
ENCODING 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8';
Also I recommend to read the following pages about locales and collations in PostgreSQL:
http://www.postgresql.org/docs/current/interactive/locale.html
http://www.postgresql.org/docs/current/interactive/collation.html
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.