Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3483724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:42:26+00:00 2026-05-18T10:42:26+00:00

In PostgreSQL, is there a way to get all of the tables that a

  • 0

In PostgreSQL, is there a way to get all of the tables that a view/table depends on based on its use of foreign keys and access to a given table?

Basically, I want to be able to copy the structure of a view/table using a script and want to be able to automatically get the list of tables that I would also need to copy in order for everything to still work right.

This response appears to be headed in the right direction, but doesn’t give me the results that I expect/need. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:42:26+00:00Added an answer on May 18, 2026 at 10:42 am

    Using the info from Andy Lester, I was able to come up with the following queries to retrieve the information that I needed.

    Get Tables that Foreign Keys refer to:

    SELECT cl2.relname AS ref_table
    FROM pg_constraint as co
    JOIN pg_class AS cl1 ON co.conrelid=cl1.oid
    JOIN pg_class AS cl2 ON co.confrelid=cl2.oid
    WHERE co.contype='f' AND cl1.relname='TABLENAME'
    ORDER BY cl2.relname;
    

    Get Tables that a View or Rules from a Table refer to:

    SELECT cl_d.relname AS ref_table
    FROM pg_rewrite AS r
    JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
    JOIN pg_depend AS d ON r.oid=d.objid
    JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
    WHERE cl_d.relkind IN ('r','v') AND cl_r.relname='TABLENAME'
    GROUP BY cl_d.relname
    ORDER BY cl_d.relname;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
That title is presumably awfully worded. I have some PostgreSQL tables. There is a
I use Postgresql 9.1. I have a table and I would like to get
Is there a way to get the hash code of a row in postgresql?
Is there a way to disable the Postgresql translation of messages? I´m running my
Is there a way to speed up PostgreSQL's createdb command? Normally I wouldn't care,
Apparently there is a database postgres that is created by default on each postgresql
I am trying to get the diff between two nearly identical tables in postgresql.
Is there any way in PostgreSQL to look up the day of the week
In a postgresql database I have 2 tables like these ones: Table items: item_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.