Re: Is there a "right" way to test if a database is empty?

From: Graham Leggett <minfrin(at)sharp(dot)fm>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Is there a "right" way to test if a database is empty?
Date: 2018-01-17 16:10:00
Message-ID: 90E4E6AA-392B-4DA1-B10D-873A0CE6031E@sharp.fm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 17 Jan 2018, at 5:47 PM, Graham Leggett <minfrin(at)sharp(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Petr Jelinek 2018-01-17 16:33:07 Re: [PATCH] session_replication_role = replica with TRUNCATE
Previous Message Konstantin Knizhnik 2018-01-17 16:09:32 Builtin connection polling