postgresql check if table exist

There are some alternatives to check if a table exist in postgresql:

So to check if exist table ‘products’ we can try these ways:

1) using pg_class:

SELECT relname FROM  pg_class WHERE relname ='products' ;

2) using information_schema.tables

To show all tables in Database it’s possible to use :

SELECT *
 FROM information_schema.tables
 WHERE table_schema='public' and table_type='BASE TABLE'

So to check if table exist

SELECT *
 FROM information_schema.tables
 WHERE table_schema='public' and table_type='BASE TABLE' and table_name='products'

3)

SELECT 1 FROM pg_catalog.pg_class
WHERE relkind = 'r' AND relname = 'products' AND pg_catalog.pg_table_is_visible(oid)
LIMIT 1

4) naive way

SELECT * FROM 'products'  LIMIT 1;

Resources:

Plpgsql 
Annunci

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

w

Connessione a %s...