我的Play 2.0数据库设置为使用PostgreSQL连接,它可以在我的有线互联网连接上正常工作。但是,当我尝试从无线互联网连接连接时,我收到无法连接到数据库[默认]错误。有人可以帮我弄明白这是为什么吗?
设定:
db.default.driver=org.postgresql.Driver
db.default.url=${DATABASE_URL}
db.default.partitionCount=1
在.bashrc文件中设置的数据库URL变量:
postgres://myusername:test123@localhost:5432/projectname
编辑:
pg_hba.conf文件:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication tvonehwegen md5
#host replication tvonehwegen 127.0.0.1/32 md5
#host replication tvonehwegen ::1/128 md5
postgresql.conf文件:
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
原来唯一的问题是,当我连接到无线网络时,我无法连接到我的本地postgreSQL数据库。有谁知道为什么会这样?谢谢!
答案 0 :(得分:1)
检查IPv6的WiFi / Internet设置。在某些网络上,除了传统的IPv4地址之外,您的计算机还会获得IPv6地址。在这种情况下,通常localhost
会映射到::1
而不是127.0.0.1
;这意味着如果您设置pg_hba.conf
以根据IPv4地址授予访问权限,则会出错。
假设防火墙关闭,例如在基于Redhat的系统service iptables stop
上,相关文件中存在以下代码段:
pg_hba.conf
:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
postgresql.conf
:
listen_addresses = '*'
您应该能够按如下方式连接到所需的数据库:
$ psql -h ::1 -U USERNAME -d DATABASE
Password for user USERNAME:
psql (9.1.11)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
DATABASE=>
和
$ psql -h 127.0.0.1 -U USERNAME -d DATABASE
Password for user USERNAME:
psql (9.1.11)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
DATABASE=>
最后Play的JDBC连接设置应该是以下形式:
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://127.0.0.1:5432/DATABASE"
db.default.user=USERNAME
db.default.password=PASSWORD
注意:我强烈建议您从不在连接字符串中使用localhost
。 99.99%的时间是127.0.0.1
。