Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

psql——查询指令

  • 列出(list)所有数据库

    /l
    
    • 等价于
      SELECT * from pg_database;
      
  • 查看(current)当前数据库

    /c
    
    • 等价于
      SELECT current_database();
      
  • 切换到某个数据库

    /c dbname
    

模式

  • 列出 所有模式
    /dn
    
    • 等价于
      SELECT schema_name
      FROM information_schema.schemata;
      

  • 列出所有表

    /dt
    
    • 等价于
      SELECT tablename 
      FROM pg_tables 
      WHERE tablename NOT LIKE 'pg%' 
          AND tablename NOT LIKE 'sql_%' 
      ORDER BY tablename;
      
  • 描述(describe)表结构

    /d tablename
    
    • 或简写
      SELECT * 
      FROM information_schema.columns 
      WHERE table_schema='public' 
          AND table_name='xxx';
      

函数、视图

  • 列出当前数据库内的所有函数

    /df
    
    • 等价于
      SELECT routine_name
      FROM information_schema.routines
      WHERE routine_type = 'FUNCTION'
          AND routine_schema = 'public';
      
  • 列出当前数据库内的所有视图

    /dv
    
    • 等价于
      SELECT * FROM pg_views 
      WHERE schemaname = 'public';
      

其他

  • 查看索引
    /di
    
    • 等价于
      SELECT * FROM pg_index;