如何使用Perl显示Postgres表

时间:2017-07-21 11:12:04

标签: database postgresql perl

我尝试在perl中使用\ d命令,这就是我得到的

DBD :: Pg :: st执行失败:ERROR:语法错误在“d”处或附近

这是我的代码

#!/usr/bin/perl

use DBI;

$myConnection = DBI->connect("DBI:Pg:dbname=test;host=localhost","postgres", "pass123");

$query = $myConnection->prepare("\d");
$result = $query->execute();


while(my @row = $sth->fetchrow_array()) {
      print "ID = ". $row[0] . "\n";
}

$myConnection->disconnect();

2 个答案:

答案 0 :(得分:2)

最简单,最便携的方法是使用table_info函数。

答案 1 :(得分:1)

如果只需要表或视图的列表,则可以使用

my @tables = $dbh->tables();

您还可以将列表限制为特定的架构。例如,仅获取public模式中的表,而不获取pg_cataloginformation_schema表:

my @tables = $dbh->tables(undef, 'public');

有关详细信息,请参见https://metacpan.org/pod/DBD::Pg#tables

有关表的更多信息,请按照Laurenz' answer的建议使用table_info