Thursday, November 25, 2021

Error while creating database in PostgreSQL

 issue:

when I tried to create new DB in postgreSQL got the below error and analyzed the issue and found the solution.

postgres_DEV=> create database postgres_db_dev1;

ERROR:  source database "template1" is being accessed by other users

DETAIL:  There is 1 other session using the database.


check which user/process holding the template DB

postgres_DEV=> select datname,pid,usename,application_name,client_hostname,client_port from pg_stat_activity where datname='template1';

  datname  | pid  | usename  |     application_name     | client_hostname | client_port

-----------+------+----------+--------------------------+-----------------+-------------

 template1 | 1871 | DB_admin | pgAdmin 4 - DB:template1 |                 |       51681

(1 row)


Solution:

Kill a  template1 session on PostgreSQL database:

select pg_terminate_backend(pid) 

from pg_stat_activity

where pid = '1871';



postgres_DEV=> select pg_terminate_backend(pid)

postgres_DEV-> from pg_stat_activity

postgres_DEV-> where pid = '1871';

 pg_terminate_backend

----------------------

 t

(1 row)


postgres_DEV=> CREATE DATABASE test2 TEMPLATE template1;


CREATE DATABASE


No comments:

Post a Comment