Re: Is there a "right" way to test if a database is empty? - Mailing list pgsql-hackers

From Graham Leggett
Subject Re: Is there a "right" way to test if a database is empty?
Date
Msg-id 90E4E6AA-392B-4DA1-B10D-873A0CE6031E@sharp.fm
Whole thread Raw
In response to Is there a "right" way to test if a database is empty?  (Graham Leggett <minfrin@sharp.fm>)
Responses Re: Is there a "right" way to test if a database is empty?
List pgsql-hackers
On 17 Jan 2018, at 5:47 PM, Graham Leggett <minfrin@sharp.fm> wrote:

I need to test whether a database is empty, in other words “createdb” has been executed but no data of any kind appears in that database.

What is the correct postgresql way to do this?

Is there a pg_isempty command or equivalent somewhere?

Does this query look right?

db=# select count(s.nspname) from pg_class c join pg_namespace s on s.oid = c.relnamespace where s.nspname in ('public');
 count 
-------
     0
(1 row)

It is based on the idea that the database is not empty if there are any class entries in the “public” namespace?

Regards,
Graham

Attachment

pgsql-hackers by date:

Previous
From: Konstantin Knizhnik
Date:
Subject: Builtin connection polling
Next
From: Petr Jelinek
Date:
Subject: Re: [PATCH] session_replication_role = replica with TRUNCATE