Thread: Check If PostgreSQL Table Exists

Check If PostgreSQL Table Exists

From
dhilchrist@gmail.com
Date:
Dear All,

How To Check If PostgreSQL Table Exists in the Database Using Perl.

Dhilchrist


Re: Check If PostgreSQL Table Exists

From
Madison Kelly
Date:
dhilchrist@gmail.com wrote:
> Dear All,
>
> How To Check If PostgreSQL Table Exists in the Database Using Perl.
>
> Dhilchrist

Here is what I do...

$DBreq=$DB->prepare("SELECT COUNT(*) FROM pg_tables WHERE
tablename='foo'") || die $DBI::errstr;
$DBreq->execute();
my ($table_num)=$DBreq->fetchrow_array();
if ( $table_num < 1 )
{
    # Create the missing table
}
else
{
    # It exists!
}

Hope that helps!

Madison