【问题标题】:How can I create a Postgres database in Perl?如何在 Perl 中创建 Postgres 数据库?
【发布时间】:2014-11-12 20:46:51
【问题描述】:

如何从 Perl 创建 Postgres 数据库?这在 Perl Postgres DBI 驱动程序DBD::Pg(或几乎其他任何地方)中没有记录。我需要能够从我的程序中创建一个临时数据库,而不必使用 psqlpgAdmin 来创建数据库。

【问题讨论】:

    标签: perl postgresql dbi


    【解决方案1】:

    诀窍是使用安装 Postgres 时可能拥有的“postgres”数据库(如果您没有“postgres”数据库,您可能知道原因)。如果您以用户 'postgres' 连接到 'postgres' 数据库,则可以发出 create database SQL 命令,如下所示:

    my $dbh = DBI->connect("dbi:Pg:dbname=postgres", "postgres");
    $dbh->do('create database "Scratch-School"');
    

    (您还可以查看create a postgreSQL database programmatically,它在更高级别上涵盖了此问题。)

    【讨论】:

    • 你应该远离像"Scratch-School"这样的带引号的标识符。许多工具都有这个问题。最好使用create database scratch_school 使名称不区分大小写,并且不需要您每次都写那些双引号。
    • 重点是要创建数据库,首先需要连接到另一个现有的数据库。它不一定是postgres,它也不是 Perl 特有的。即使psql 也是如此,甚至createdb 本身也是如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2019-05-19
    • 2014-01-15
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    相关资源
    最近更新 更多