2012-05-15 9 views

答えて

1

、次のように入力することができます:あなたはSQLを実行している場合

\d 

http://www.postgresql.org/docs/9.1/static/app-psql.html

は、次のように入力することができます:あなたがしたい場合

SELECT * FROM tables; 

http://www.postgresql.org/docs/current/interactive/information-schema.html

その使用状況に関する統計情報を入力することができます:

SELECT * FROM pg_stat_user_tables; 

http://www.postgresql.org/docs/current/interactive/monitoring-stats.html

2

質問はpostgresので物事を行うにはPythonのpsycopg2の使用についてです。ここには便利な機能が2つあります:

def table_exists(con, table_str): 

    exists = False 
    try: 
     cur = con.cursor() 
     cur.execute("select exists(select relname from pg_class where relname='" + table_str + "')") 
     exists = cur.fetchone()[0] 
     print exists 
     cur.close() 
    except psycopg2.Error as e: 
     print e 
    return exists 

def get_table_col_names(con, table_str): 

    col_names = [] 
    try: 
     cur = con.cursor() 
     cur.execute("select * from " + table_str + " LIMIT 0") 
     for desc in cur.description: 
      col_names.append(desc[0])   
     cur.close() 
    except psycopg2.Error as e: 
     print e 

    return col_names 
+3

良い答え

conn = psycopg2.connect(conn_string) cursor = conn.cursor() cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';") print cursor.fetchall() 

出力リストにタプルとして、ユーザ定義のテーブルを返します。以下のクエリを実行

間違った質問のために。 –

22

pg_classには必要な情報がすべて格納されています。

[('table1',), ('table2',), ('table3',)] 
+1

アレイフォームでどうやって取得できますか? – ihue

20

これやった私のためのトリック:

cursor.execute("""SELECT table_name FROM information_schema.tables 
     WHERE table_schema = 'public'""") 
for table in cursor.fetchall(): 
    print(table) 
+1

ポイントとユニバーサルに感謝kalu rigth。 – peter