以php中的thinkphp 5.1为例,php容器连接mac中的pgsql数据库失败时,出现如下错误
[7] PDOException in Connection.php line 528
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
解决:以thinkphp 5.1为例,只需把 database.php 中的 hostname 改为 host.docker.internal 即可,然后,thinkphp 就能访问到数据库了。
return [
// 数据库类型
'type' => 'pgsql',
// 服务器地址
'hostname' => 'host.docker.internal'
];
如果可以访问后,再出现 table_msg(unknown) 如下错误时
SQLSTATE[42883]: Undefined function: 7 ERROR: function table_msg(unknown) does not exist
LINE 1: ...fault as "default",fields_default as "extra" from table_msg(...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
解决:在数据库中执行如下sql即可(因为 table_msg 函数需要用户自己定义)
CREA