星期二, 9月 08, 2009

postgresql 對應 mysql

For those coming from MySQL:
SHOW TABLES = \d
SHOW DATABASES = \l
SHOW COLUMNS = \d table

However the \* commands only work in psql and not via other interfaces, such as queries via PHP. Similar data can be retrieved with the following SQL commands:

SHOW TABLES (\d) = SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'

SHOW DATABASES (\l) = SELECT datname FROM pg_database;

SHOW COLUMNS FROM table (\d table) = SELECT column_name FROM information_schema.columns WHERE table_name ='table';

To get the column names in their "natural" order (as in, the order they were created), use:
SELECT column_name FROM information_schema.columns WHERE table_name ='< table >' ORDER BY ordinal_position;


Alternatively you could send psql metacommands to psql directly:

SHOW TABLES -> echo "\d" | psql "database"

Use "psql -tA" for script friendly format.