使用JDBC连接到PostgreSql的本地实例 [英] Connecting to local instance of PostgreSql with JDBC

查看:443
本文介绍了使用JDBC连接到PostgreSql的本地实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux机器上有一个正在运行的PostgreSql本地实例.当我从外壳使用psql命令时,我可以成功登录而没有任何问题.我需要通过JDBC连接到PostgreSql,但是我不知道到底应该将什么作为url参数传递给DriverManager.getConnection().

I have a running local instance of PostgreSql on a linux machine. When I use psql command from the shell I success to log in without any problem. I need to connect to the PostgreSql via the JDBC, but I don't know what exactly should I pass as url parameter to DriverManager.getConnection().

它应该以jdbc:postgresql:开头,但是接下来要做什么?

It should start with jdbc:postgresql: but what's going next?

系统组告诉我,创建了一个数据库,例如用户名.例如如果我的用户是jutky,则创建了一个名为jutky的数据库,但是当我尝试打开与jdbc:postgresql:jutky的连接时会出现错误

I was told by the system group that a database with was created like user name. e.g. if my user is jutky a db named jutky was created, but when I try to open a connection to jdbc:postgresql:jutky I get an error

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "jutky" :(

其他信息

当我通过psql登录时,没有提示输入密码,因此当我尝试通过JDBC登录时,我传递了一个空字符串作为密码-是否正确,还是应该传递null之类的密码? ?

When I login via the psql I'm not prompted for the password, so when I try to login via JDBC I pass an empty string as a password - is it correct, or should I pass null or something?

当我在外壳中键入psql --help时,我会在剩下的这一行中看到:

When I type psql --help in the shell I see among the rest this line:

Connection options:
   -h, --host=HOSTNAME      database server host or socket directory (default: "/var/run/postgresql")

所以我知道我是通过socket directory连接到PostgreSql的,这对JDBC中的URL字符串有影响吗?

So I understand that I connect to PostgreSql via a socket directory, does that matters something to the URL string in the JDBC?

编辑

首先感谢您的回答.

第二:我不是第一次使用JDBC,特别是不是第一次从JDBC连接到PostgreSql,所以我知道一般规则,并且已经阅读了文档.但是在所描述的情况下,如果实例通过socket directory运行,我不确定应该如何构造连接字符串以及应该提供什么密码.因为当我通过psql登录时,根本没有提示输入密码.

Second: its not first time I'm using JDBC and in particular not the first time I'm connecting to the PostgreSql from JDBC, so I know the general rules and I have read the documentations. However in the described case I'm not sure how exactly should I construct the connection string if the instance is running via the socket directory and what password should I provide. Because when I login via the psql I'm not prompted for password at all.

谢谢.

推荐答案

除其他答案外,默认情况下,Postgres配置为通过基于您的操作系统帐户的身份验证通过Unix套接字接受连接,这就是psql起作用的原因很好,不需要密码.

In addition to other answers note that by default Postgres is configured to accept connections via Unix sockets with authentication based on your operating system account, that's why psql works fine and doesn't require the password.

JDBC连接是通过具有密码身份验证的TCP/IP建立的,因此您需要相应地修改pg_hba.conf.例如,此行允许所有具有密码身份验证的用户从同一台计算机到所有数据库的TCP/IP连接:

JDBC connections are made over TCP/IP with password authentication, so you need to modify pg_hba.conf accordingly. For example, this line allows TCP/IP connections from the same machine to all databases for all users with password authentication:

host    all         all         127.0.0.1/32          md5

添加此行后,jdbc:postgresql:databasename应该可以使用.

After adding this line jdbc:postgresql:databasename should work.

编辑:由于PostgreSQL JDBC驱动程序只能在TCP/IP上运行,因此无法通过Unix套接字创建JDBC连接.创建JDBC连接时使用的密码是分配给您的用户的密码.如果没有它,则可以使用例如ALTER USER命令进行分配.参见 19.3.身份验证方法.

You can't create a JDBC connection over Unix socket since PostgreSQL JDBC driver can only work over TCP/IP. The password you use when creating JDBC connection is the password assigned to your user. If you don't have it, you can assign it, for example, using ALTER USER command. See 19.3. Authentication methods.

另请参见:

这篇关于使用JDBC连接到PostgreSql的本地实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆