[英]Check if database exists in postgreSQL using PHP
I was wondering if anyone would be able to tell me about whether it is possible to use PHP to check if a postgresql database exists? 我想知道是否有人可以告诉我是否可以使用PHP检查postgresql数据库是否存在?
I am writing a PHP script and I only want it to create the database if it doesn't already exist but up to now haven't been able to see how to implement it. 我正在编写一个PHP脚本,并且只希望它创建尚不存在的数据库,但是到目前为止还无法看到如何实现它。
Thank you 谢谢
I've tried 我试过了
$cmd = 'psql -U postgres -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'portal';"';
exec($cmd);
I got 我有
PHP Parse error: syntax error, unexpected 'portal'
You have a syntax error singe the single quote is used to initalize the statement. 您有语法错误,单引号用于初始化语句。
$cmd = 'psql -U postgres -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = \'portal\';"';
exec($cmd);
You should use PDO instead of exec, but this would probably work for you. 您应该使用PDO而不是exec,但这可能对您有用。
You are confusing schema
and database
. 您在混淆
schema
和database
。 in postgres it is schema what you call database in mysql. 在postgres中,您在mysql中称为数据库。 so if you want to find a database, better use
pg_databases
因此,如果要查找数据库,最好使用
pg_databases
$cmd = 'psql -U postgres -c "select \'already_there\' from pg_database where datname = \'db1\';"';
exec($cmd);
这是PHP错误,您必须转义'\\'portal \\''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.