
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com
Mobile version of this page
Previous:
Google blogs about their Maps
Next:
Serious flaw in Bose headphones
Creating a user for postgresql
Adding a year in PostgreSQL
Date formatting in python or in PostgreSQL
Integer division in programming languages
Running simple SQL commands on the command line
List of casts in PostgreSQL
Just Oracle and IBM?
ALTER TABLE patch
Quick PostgreSQL optimization story
Date formatting in Python or in PostgreSQL (part II)
Speed test between django_mongokit and postgresql_psycopg2
UPPER vs. ILIKE
Fastest "boolean SQL queries" possible with Django
To sub-select or not sub-select in PostgreSQL
Sorting transform function in PostgreSQL
Why bother with MySQL...
Optimization of getting random rows out of a PostgreSQL in Django
Connecting with psycopg2 without a username and password
Who was logged in during a Django exception
Google blogs about their Maps
Next:
Serious flaw in Bose headphones
Related blogs
PostgreSQL, MySQL or SQLiteCreating a user for postgresql
Adding a year in PostgreSQL
Date formatting in python or in PostgreSQL
Integer division in programming languages
Running simple SQL commands on the command line
List of casts in PostgreSQL
Just Oracle and IBM?
ALTER TABLE patch
Quick PostgreSQL optimization story
Date formatting in Python or in PostgreSQL (part II)
Speed test between django_mongokit and postgresql_psycopg2
UPPER vs. ILIKE
Fastest "boolean SQL queries" possible with Django
To sub-select or not sub-select in PostgreSQL
Sorting transform function in PostgreSQL
Why bother with MySQL...
Optimization of getting random rows out of a PostgreSQL in Django
Connecting with psycopg2 without a username and password
Who was logged in during a Django exception
Related by category
pg_class to check if table exists
http://www.vitavoom.com/postgresql-docs/catalog-pg-class.html20th of April 2005
I just learnt a better way to check if a PostgreSQL table exists. Before I did this to find out if table mytable is defined:
SELECT * FROM 'mytable' LIMIT 1;
But this had to be wrapped in exception catching application code because I was able to tell if the table existed if the select statement worked. That's the wrong and naive way. The correct way is to use pg_class and look at how many rows were returned:
SELECT relname FROM pg_class
WHERE relname = 'mytable';
WHERE relname = 'mytable';
Why have I missed this before? No explanation?
A disadvantage with using pg_class is that it's not ISO standard, meaning that it's specific to PostgreSQL only. The first pattern will work on all relational databases.
Comment
dd -
10th May 2006
[«« Reply to this]
For functionality similar to mysql SHOW TABLES use: select * from information_schema.tables where table_schema='public' and table_type='BASE TABLE' .
For functionality similar to mysql SHOW TABLES use: select * from information_schema.tables where table_schema='public' and table_type='BASE TABLE' .
toruvinn -
4th September 2006
[«« Reply to this]
btw. there's a problem with temporary tables, cause they might not be visible (even though they exist). it's better to use:
SELECT 1 FROM pg_catalog.pg_class WHERE relkind = 'r' AND relname = 'name' AND pg_catalog.pg_table_is_visible(oid) LIMIT 1
btw. there's a problem with temporary tables, cause they might not be visible (even though they exist). it's better to use:
SELECT 1 FROM pg_catalog.pg_class WHERE relkind = 'r' AND relname = 'name' AND pg_catalog.pg_table_is_visible(oid) LIMIT 1


In psql do "\set ECHO_HIDDEN t" and you can see all the queries it uses for the special backslash commands (like \d)