Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch all tables in a particuler Postgres database using node?

Tags:

I need to fetch all tables in a particular Postgres database using node. But not finding any way to achieve that. Is there any way to get that?

For example, suppose I have a database named 'TestDatabase' it contains 4 tables( just assume it can have less or more) Person, Company, Clothes, Animal. I need to get the name of all of them using node.

I am also using node-postgres ('pg') to connect with the database.

like image 462
Anuresh Verma Avatar asked Oct 16 '20 12:10

Anuresh Verma


1 Answers

As an alternative you can use information_schema. It is not better then the pg_* objects in the system catalog but is standardized and more portable. Here it is:

select table_schema||'.'||table_name as table_fullname
 from information_schema."tables"
 where table_type = 'BASE TABLE'
  and table_schema not in ('pg_catalog', 'information_schema');

The system objects have been filtered by this expression

table_schema not in ('pg_catalog', 'information_schema')

You can further modify it to only include schemas that you need.

like image 191
Stefanov.sm Avatar answered Oct 18 '22 08:10

Stefanov.sm