python如何遍历postgresql所有的用户表

作者:

文章目录
  • SELECT table_name FROM information_schema.tables WHERE table_schema = ‘public’ — 只查询 public 模式,可修改为其他模式 AND table_type = ‘BASE TABLE’; — 只查询用户表,排除视图等
  • SELECT relname AS table_name FROM pg_class JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid WHERE pg_namespace.nspname = ‘public’ — 模式名 AND pg_class.relkind = ‘r’; — ‘r’ 表示普通表
  • 在 psql 交互式终端中,可以直接使用: dt  — 显示当前模式下的所有表 dt *.*  — 显示所有模式下的所有表
  • 如果你需要在代码中动态遍历表,可以使用以下 Python 代码: import psycopg2 def get_all_tables(database, user, password, host=”localhost”, port=”5432″): try: # 连接到 PostgreSQL 数据库 connection = psycopg2.connect( database=database, user=user, password=password, host=host, port=port ) cursor = connection.cursor() # 查询所有用户表 query = “”” SELECT table_name FROM information_schema.tables WHERE table_schema = ‘public’ AND table_type = ‘BASE TABLE’; “”” cursor.execute(query) # 获取所有表名 tables = [row[0] for row in cursor.fetchall()] return tables except (Exception, psycopg2.Error) as error: print(“Error while connecting to PostgreSQL”, error) finally: # 关闭数据库连接 if connection: cursor.close() connection.close() # 使用示例 if __name__ == “__main__”: tables = get_all_tables( database=”your_database”, user=”your_username”, password=”your_password” ) print(“所有用户表:”, tables) 说明 模式过滤:上述示例默认只查询 public 模式下的表。如果你有其他模式(如 myschema),需要修改 table_schema = 'public' 或 nspname = 'public'。 系统表排除:通过 table_type = 'BASE TABLE' 或 relkind = 'r' 确保只返回用户创建的普通表,不包括视图、索引等。 权限要求:需要有访问 information_schema 或 pg_class 的权限,通常普通用户都具备此权限。 根据你的具体需求选择合适的方法即可。 到此这篇关于python如何遍历postgresql所有的用户表的文章就介绍到这了,更多相关python遍历postgresql用户表内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: 使用python进行PostgreSQL数据库连接全过程 Python连接和操作PostgreSQL数据库的流程步骤 安装python依赖包psycopg2来调用postgresql的操作 Python操作PostgreSql数据库的方法(基本的增删改查) Python实现连接postgresql数据库的方法分析 Python连接PostgreSQL数据库的方法
  • 目录
    • 方法一:使用 SQL 查询
    • 方法二:查询系统表
    • 方法三:使用 psql 元命令(命令行工具)
    • 方法四:通过编程语言遍历(Python 示例)

    要遍历 PostgreSQL 中的所有用户表,你可以通过查询系统目录表 pg_class 结合 pg_namespace 来实现。以下是几种常见的方法:

    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = 'public'  -- 只查询 public 模式,可修改为其他模式
      AND table_type = 'BASE TABLE';  -- 只查询用户表,排除视图等
    

    SELECT relname AS table_name
    FROM pg_class
    JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
    WHERE pg_namespace.nspname = 'public'  -- 模式名
      AND pg_class.relkind = 'r';  -- 'r' 表示普通表
    

    在 psql 交互式终端中,可以直接使用:

    • dt  — 显示当前模式下的所有表
    • dt *.*  — 显示所有模式下的所有表

    如果你需要在代码中动态遍历表,可以使用以下 Python 代码:

    import psycopg2
    
    def get_all_tables(database, user, password, host="localhost", port="5432"):
        try:
            # 连接到 PostgreSQL 数据库
            connection = psycopg2.connect(
                database=database,
                user=user,
                password=password,
                host=host,
                port=port
            )
            cursor = connection.cursor()
            
            # 查询所有用户表
            query = """
                SELECT table_name
                FROM information_schema.tables
                WHERE table_schema = 'public'
                  AND table_type = 'BASE TABLE';
            """
            cursor.execute(query)
            
            # 获取所有表名
            tables = [row[0] for row in cursor.fetchall()]
            return tables
            
        except (Exception, psycopg2.Error) as error:
            print("Error while connecting to PostgreSQL", error)
        finally:
            # 关闭数据库连接
            if connection:
                cursor.close()
                connection.close()
    
    # 使用示例
    if __name__ == "__main__":
        tables = get_all_tables(
            database="your_database",
            user="your_username",
            password="your_password"
        )
        print("所有用户表:", tables)
    

    说明

    • 模式过滤:上述示例默认只查询 public 模式下的表。如果你有其他模式(如 myschema),需要修改 table_schema = 'public' 或 nspname = 'public'。
    • 系统表排除:通过 table_type = 'BASE TABLE' 或 relkind = 'r' 确保只返回用户创建的普通表,不包括视图、索引等。
    • 权限要求:需要有访问 information_schema 或 pg_class 的权限,通常普通用户都具备此权限。

    根据你的具体需求选择合适的方法即可。

    到此这篇关于python如何遍历postgresql所有的用户表的文章就介绍到这了,更多相关python遍历postgresql用户表内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

    您可能感兴趣的文章:

    • 使用python进行PostgreSQL数据库连接全过程
    • Python连接和操作PostgreSQL数据库的流程步骤
    • 安装python依赖包psycopg2来调用postgresql的操作
    • Python操作PostgreSql数据库的方法(基本的增删改查)
    • Python实现连接postgresql数据库的方法分析
    • Python连接PostgreSQL数据库的方法

    站内搜索