PostgreSQL: Show tables in PostgreSQL

后端 未结 24 2589
醉梦人生
醉梦人生 2020-11-28 16:58

What\'s the equivalent to show tables (from MySQL) in PostgreSQL?

24条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 17:48

    as a quick oneliner

    # just list all the postgres tables sorted in the terminal
    db='my_db_name'
    clear;psql -d $db -t -c '\dt'|cut -c 11-|perl -ne 's/^([a-z_0-9]*)( )(.*)/$1/; print'
    

    or if you prefer much clearer json output multi-liner :

    IFS='' read -r -d '' sql_code <<"EOF_CODE"
        select array_to_json(array_agg(row_to_json(t))) from (
            SELECT table_catalog,table_schema,table_name 
            FROM information_schema.tables
            ORDER BY table_schema,table_name ) t
    EOF_CODE
    psql -d postgres -t -q -c "$sql_code"|jq
    

提交回复
热议问题