Search code examples
node.jspostgresqlnode-postgrespostgresql-12

Fetch all tables in a particuler Postgres database using node?


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.


Solution

  • 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.