2

I need to loop through all the table in a database in postgresql. is there a similar stored procedure like mssql sp_msforeachtable for postgresql?

2 Answers 2

3

I usually use this query against information_schema.tables:

SELECT table_name
FROM information_schema.tables
WHERE table_type   = 'BASE TABLE'
  AND table_schema = 'public'
ORDER BY table_name

You might want to adjust the table_schema to suit your needs though. This query should (AFAIK) work in any database that follows the standard.

Sign up to request clarification or add additional context in comments.

Comments

2

Look in the information schema: information_schema.tables.

2 Comments

How do I loop through all the tables then process the table, if I lookup in the information_schema.tables I still need to use a script to loop the returned results and execute / process each table. In ms sql you can just do something like this in the command line (sqlcmd) : sp_msforeachtable 'delete from ?' that command will delete columns from all the tables in a database
@technomage, you need to learn how to use UNIX. psql -At -c 'SELECT table_name FROM information_schema.tables' | xargs -n 1 ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.