简体   繁体   English

将PHP连接到Postgres

[英]Connecting php to postgres

I have a VM running PostgreSQL (pgAdmin III to be specific) and I am attempting to use red bean to connect to it with no success. 我有一个运行PostgreSQL的VM(具体来说是pgAdmin III),并且我试图使用Red bean连接到它没有成功。 It does appear to connect, yet when I send something for the DB to store I get an error... Here is my code: 它似乎确实在连接,但是当我发送一些东西供数据库存储时,我得到一个错误...这是我的代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include 'php/third-party/rb.php';
R::setup('pgsql:host=localhost;dbname=mydb',
        '(DBUser)','(MyPassword)');
$book = R::dispense( 'book' );
$book->title = 'Learn to Program';
$book->rating = 10;
$book['price'] = 29.99;
$id = R::store( $book );
?>

I don't think it has anything to the code. 我认为代码没有任何意义。 Here is the error that is getting thrown: 这是将引发的错误:

"Fatal error: Uncaught exception 'PDOException' with message 'Could not connect to database (mydb).'..." “致命错误:消息为“无法连接到数据库(mydb)的未捕获异常'PDOException'。'...”

Here are some points to consider: 这里有几点要考虑:

  1. What do you mean with that "the code appears to connect"? “代码似乎已连接”是什么意思? The messages says "Could not connect..." The R::setup and R::dispense calls don't attempt to connect. 消息显示“无法连接...” R :: setup和R :: dispense调用不会尝试连接。 R::store calls attempts to connect. R :: store调用尝试连接。 That's where your error pops up, I would think. 我想这就是错误出现的地方。

  2. As comment from @ChrisForrence asks: are you running the script on the same machine as the db server? 正如@ChrisForrence的评论所问:您是否在与db服务器相同的计算机上运行脚本? If not you have to change 'localhost' in the connect string. 如果不是,则必须在连接字符串中更改“ localhost”。

  3. Can you connect using the standard postgres cli tool (psql)? 您可以使用标准的postgres cli工具(psql)进行连接吗?

  4. Do you have the PDO postgresql drivers installed? 是否安装了PDO postgresql驱动程序?

  5. Are you running in a SELinux environment and is your script being run by the apache process? 您是否正在SELinux环境中运行,并且脚本是否正在由apache进程运行? If so, does the security context of the apache process allow it to make connections to the postgresql server? 如果是这样,那么apache进程的安全性上下文是否允许它与PostgreSQL服务器建立连接?

Check these issues. 检查这些问题。

This looks a bit weird: 这看起来有点奇怪:

R::setup('pgsql:host=localhost;dbname=mydb',
    '(DBUser)','(MyPassword)');

try removing the parenthesis around DBUser and MyPassword . 尝试删除围绕DBUserMyPassword的括号。

If that still doesn't fix it, your php probably doesn't have php5-pgsql installed. 如果仍然不能解决问题,则您的php可能没有安装php5-pgsql。

You can install it with this command (assuming your server is a debian flavour): 您可以使用以下命令进行安装(假设您的服务器具有Debian风格):

sudo apt-get install php5-pgsql

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

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