Error template database template1 does not exist

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

@amolkhanorkar

Last active

November 25, 2022 01:57

Star

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


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

Show hidden characters

======= Prolbem =================================================================================================================
I have installed : ruby-2.0.0,postgres-9.2 , now in rails app when I execute:
rake db:create , command I get:
PG::InvalidParameterValue: 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 «my_db_name» ENCODING = ‘unicode’…….
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»}
=================================================================================================================================
Solution
=================================================================================================================================
Ok, below steps resolved the problem:
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’;
Now we can drop it:
DROP DATABASE template1;
Now its time to create database from template0, with a new default encoding:
CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = ‘UNICODE’;
Now modify template1 so it’s actually a template:
UPDATE pg_database SET datistemplate = TRUE WHERE datname = ‘template1’;
Now switch to template1 and VACUUM FREEZE the template:
c template1
VACUUM FREEZE;
Problem should be resolved.

Pivotal Greenplum is one of widely used MPP relational databases. You can handle huge amount of data with Greenplum. Sometimes, it is very difficult to create a database in the Greenplum server because of connection issue. In this article, we will check how to resolve Greenplum create database error and steps required to resolve.

Greenplum Create Database Error

Below are the most common database errors that you may face while creating a database on Greenplum server.

  • psql: FATAL: database “gpadmin” does not exist
  • ERROR: source database “template1” is being accessed by other users

Now let us check what are these errors in detail.

psql: FATAL: database “gpadmin” does not exist

The first error is because, by default, “gpadmin”, database is not created when you set up the Greenplum server. So when you try to connect to non-existent Greenplum ‘gpadmin’ database either using an application or psql client, you will end up getting database not found fatal error.

ERROR: source database “template1” is being accessed by other users

You will probably get this kind of error when you try to create any new database from psql. There will be three databases already created for you, template0, template1, or postgres.

Initially, when you try to connect to Greenplum using the psql client, you should provide a database name to which you are trying to connect. You may end up getting “source database “template1” is being accessed by other users” when you try to create new a database by connecting to template1 database.

Database template1 exists only to provide structure to create another empty database. You should never log on to template1, otherwise you will have problems.

Below are the steps that you can follow to resolve this issue.

  • Restart Greenplum server. When the server is up connect to Postgres database and create new database.
  • If restarting is not an option, you can use another emergency template database: template0.
  • If the second option fails, it might, probably. then Third option would be kill the sessions that are connected to template0, or template1.

Kill Greenplum Sessions

You can follow below commands to kill Greenplum sessions that are connected to either template0 or template1.

  • Query pg_stat_activity table to identify the active or idle sessions. For example
postgres=# select datname, procpid  FROM pg_stat_activity  WHERE usename='gpadmin';
  datname  | procpid
-----------+---------
 postgres  |   24949
 template1 |   29232
 template1 |   29233
  • Kill background sessions using pg_terminate_backend. Connect to postgres database and kill other sessions.
 postgres=# select pg_terminate_backend(24967);
 pg_terminate_backend
----------------------
 t
(1 row)

postgres=# select pg_terminate_backend(24973);
 pg_terminate_backend
----------------------
 t
(1 row)
  • Now, you can create new database

Related Articles

  • Greenplum Create, Rename, Drop Database and Examples

Hope this helps 🙂

CREATE DATABASE actually works by copying an existing database. 
two defalut database when initializing cluster

  1. TEMPLATE1
  2. TEMPLATE0

TEMPLATE1:

  • By default, it copies the standard system database named template1.
  • If you add objects to template1, these objects will be copied into subsequently created user databases.
  • if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.
  • template1 might contain encoding-specific or locale-specific data

TEMPLATE0:

  • TEMPLATE1 contains the same data as the initial contents of template1.
  • template0 should never be changed after the database cluster has been initialized becouse pg_database.datistemplate = false.
  • template0 might not contain encoding-specific or locale-specific data.

Note: 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. This course of action might be advisable if one has carelessly added a bunch of junk in template1.(To delete template1, it must have pg_database.datistemplate = false.) 
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.
PRACTICAL FOR TEMPLATE0:
—If you try connect template0 database it will throw error because you cannot modify template0 database by default

postgres=# c template0
FATAL: database "template0" is not currently accepting connections
Previous connection kept

—To create a database by copying template0, use from the SQL environment:

postgres=# create database temp0 template template0;
CREATE DATABASE

—From linux command line mode

[postgres@r1 bin]$ ./createdb -T template0 temp0_1
Password:

—list the total database

[postgres@r1 bin]$ ./psql -l
Password:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+----------+----------+----------------+-------------+-----------------------
account | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgresclone | clone | UTF8 | en_US.UTF- 8 | en_US.UTF-8 |
temp0 | u2 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp0_1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp0copy | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp0copy2 | 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
(9 rows)

PRACTICAL FOR  TEMPLATE1:
—YOU can connect template1 database and you can modify or create  any objects but these objects will be copied into subsequently created user databases

[postgres@r1 bin]$ ./psql -d template1 -U postgres
Password for user postgres:
psql.bin (9.3.14)
Type "help" for help.

No entry for terminal type "xterm";
using dumb terminal settings.
template1=# dt

—Checking any tables in template1 database

template1-# dt   
No relations found.

—Now creating objects in template1 database

template1=# create table temp1table(id int);
CREATE TABLE

—then create a new database

template1=# create database temp1db;
CREATE DATABASE

—Now connect the temp1db and check the already created table of «temp1table» on temp1db

temp0db=# create database temp1db;
CREATE DATABASE

temp0db=# c temp1db
You are now connected to database "temp1db" as user "postgres".

temp1db=# dt
List of relations
Schema | Name | Type | Owner
--------+------------+-------+----------
public | temp1table | table | postgres
(1 row)

Two useful flags exist in pg_database for each database: 
the columns 

  1. datistemplate
  2. datallowconn

1.datistemplate:
datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. 
Both template0 and template1 should always be marked with datistemplate = true.
To delete template1, it must have pg_database.datistemplate = false.
2.datallowconn:
If datallowconn  is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). 
The template0 database is normally marked datallowconn = false to prevent its modification. 

PRACTICAL FOR PG_DATABASE(DATISTEMPLATE,DATAALLOWCONN):
DATISTEMPLATE=TRUE/FALSE:

postgres=# select datname,datistemplate,datallowconn from pg_database;
datname | datistemplate | datallowconn
---------------+---------------+--------------
template1 | t | t
template0 | t | f
postgres | f | t
account | f | t
temp0copy | f | t
temp0copy2 | f | t
postgresclone | f | t
temp0 | f | t
temp0_1 | f | t
temp0db | f | t
temp1db | f | t
temp1 | f | t
(12 rows)
  • if we want to drop any database means you need to set datistemplate=false
  • if you want to connect to template0 you need to set dataallowcomm=true

—when you drop template1 database you cannot drop becouse datistemplate=true

postgres=# drop database template1;
ERROR: cannot drop a template database

—changing datistemplate values

postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template1';
UPDATE 1

—Now check the datistemplate value 

postgres=# select datname,datistemplate,datallowconn from pg_database;          
datname | datistemplate | datallowconn
---------------+---------------+--------------
template0 | t | f
postgres | f | t
account | f | t
temp0copy2 | f | t
postgresclone | f | t
temp0 | f | t
temp0_1 | f | t
temp0db | f | t
temp1db | f | t
temp1 | f | t
template1 | f | t
(11 rows)

—Now drop the template1 database

postgres=# drop database template1;
DROP DATABASE

Recreating template1 database from template0
DATALLOWCONN=TRUE/FALSE:
—Check the datistemplate value

postgres=# select datname,datistemplate,datallowconn from pg_database;          
datname | datistemplate | datallowconn
---------------+---------------+--------------
template0 | t | f
postgres | f | t
account | f | t
temp0copy2 | f | t
postgresclone | f | t
temp0 | f | t
temp0_1 | f | t
temp0db | f | t
temp1db | f | t
temp1 | f | t
template1 | f | t
(11 rows)

—the above table datallow connection is false on template0 database so we cannot connect to template0 databse we already disscussed above example if we want to connect template0 database you need to set datallowconn=true

template1=# c template0
FATAL: database "template0" is not currently accepting connections
Previous connection kept

—Changing datallowconn to true

postgres=# UPDATE pg_database SET datallowconn='true' WHERE datname='template0';
UPDATE 1

—Now check the datallowconn value

postgres=# select datname,datistemplate,datallowconn from pg_database;          
datname | datistemplate | datallowconn
---------------+---------------+--------------
postgres | f | t
account | f | t
temp0copy2 | f | t
postgresclone | f | t
temp0 | f | t
temp0_1 | f | t
temp0db | f | t
temp1db | f | t
temp1 | f | t
template1 | f | t
template0 | t | t
(11 rows)

—Now you can connect template0 database and also you can create database objects

template1=# c template0
You are now connected to database "template0" as user "postgres".

template0=# create table ti(id int);
CREATE TABLE

SCENARIO FOR TEMPLATE  Database
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose «COPY DATABASE» facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.

postgres=# create database temp1 template template1;
ERROR: source database "template1" is being accessed by other users
DETAIL: There is 1 other session using the database.

solution:
To clone an existing database with postgres you can do that
—KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (sourcedb)
Syntax:
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = ‘SOURCE_DB’ AND pid <> pg_backend_pid();

postgres=# SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'template1' AND pid <> pg_backend_pid();
pg_terminate_backend
----------------------
t
(1 row)

—CLONE DATABASE TO NEW ONE(TARGET_DB) 
Syntax:
CREATE DATABASE TARGET_DB WITH TEMPLATE SOURCE_DB OWNER USER_DB;
IT will kill all the connection to the source db avoiding the error

CREATE TABLE employees OF employee_type (
PRIMARY KEY (name),
salary WITH OPTIONS DEFAULT 1000

Basic Error:

[root@richards bin]# ./createdb -T template0 temp0
Password:
Password:
createdb: could not connect to database template1: FATAL: password authentication failed for user "root"

Solution:
 you run this commnad as root user that is why you are facing this error so you need to run this above command as postgres user

[root@richards bin]# su postgres
bash-3.2$ cd /opt/PostgreSQL/9.4/bin/

bash-3.2$ ./createdb -T template0 temp0
createdb: database creation failed: ERROR: database "temp0" already exists
bash-3.2$ ./createdb -T template0 temp01

bash-3.2$ ./psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privil
eges
------------+----------+----------+-------------+--------------+----------------
-------
db_to_drop | postgres | UTF8 | en_US.UTF-8 | en_US.UTF -8 |
dinesh | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
richards | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp01 | 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/po
stgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/po
stgres
(8 rows)

Понравилась статья? Поделить с друзьями:
  • Error temp too high
  • Error temp lost перевод
  • Error temp lost antminer t17
  • Error telebot break infinity polling
  • Error telebot a request to the telegram api was unsuccessful error code 409