pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: H <agents@meddatainc.com>
To: PostgreSQL Users Mailing List <pgsql-general@lists.postgresql.org>
Subject: Dropping all tables in a database
Date: Sun, 6 Aug 2023 21:17:40 -0400
Message-ID: <ca828fc5-4ab6-f365-cb69-8076dee6e808@meddatainc.com> (raw)

I am running PostgreSQL  13.11 and tried to drop all tables in a database without dropping the database or schema. After logging in as the correct user, the following SQL statement does not work:

SELECT 'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;' FROM pg_tables WHERE schemaname = 'public' AND tableowner = 'xxx';

The above statement does not drop any tables, nor are there any error messages.

SELECT * FROM pg_tables;

The above shows all tables are still present in the database.

Dropping individual tables works fine but since I need to drop all tables in the database in a develop environment, this is not workable.

I had to resort to the following:

-- turn off headers:
\t
SELECT 'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;' FROM pg_tables WHERE schemaname = 'public' AND tableowner = 'livraddarpaket';
\g out.tmp
\i out.tmp

The SQL statements above run fine.

Is there some setting I have to change in the database to have the first SQL statement to work or have I run into a possible bug?








view thread (7+ messages)  latest in thread

Message-ID: <ca828fc5-4ab6-f365-cb69-8076dee6e808@meddatainc.com>
Permalink:  ../ca828fc5-4ab6-f365-cb69-8076dee6e808@meddatainc.com/
Also on:    postgresql.org/message-id/ca828fc5-4ab6-f365-cb69-8076dee6e808@meddatainc.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: agents@meddatainc.com
  Subject: Re: Dropping all tables in a database
  In-Reply-To: <ca828fc5-4ab6-f365-cb69-8076dee6e808@meddatainc.com>
  References: <ca828fc5-4ab6-f365-cb69-8076dee6e808@meddatainc.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox