简体   繁体   English

如何在Perl中创建Postgres数据库?

[英]How can I create a Postgres database in Perl?

How can I create a Postgres database from Perl? 如何从Perl创建Postgres数据库? This is not documented in the Perl Postgres DBI driver DBD::Pg (or pretty much anywhere else). Perl Postgres DBI驱动程序DBD::Pg (或其他任何地方)都没有记录这一点。 I need to be able to create a scratch database from my program, rather than having to use psql or pgAdmin to create the database. 我需要能够从我的程序创建一个临时数据库,而不必使用psqlpgAdmin来创建数据库。

The trick is using the 'postgres' database that you will likely have with your installation of Postgres (if you don't have a 'postgres' database, you probably know why). 诀窍是使用你可能在安装Postgres时使用的'postgres'数据库(如果你没有'postgres'数据库,你可能知道为什么)。 If you connect as user 'postgres' to the 'postgres' database, you can then issue then create database SQL command, like so: 如果以“postgres”用户身份连接到“postgres”数据库,则可以发出create database SQL命令,如下所示:

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

(You might also look at create a postgreSQL database programmatically , which covers this issue at a higher level.) (您可能还会以编程方式创建一个postgreSQL数据库 ,它将在更高级别上解决此问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM