简体   繁体   English

在 AWS EC2 中将 Rstudio 与 PostgreSQL 连接

[英]Connecting Rstudio with PostgreSQL in AWS EC2

I am currently running PostgreSQL server in AWS EC2 (Ubuntu) and having trouble accessing it from local server.我目前在 AWS EC2 (Ubuntu) 中运行 PostgreSQL 服务器,并且无法从本地服务器访问它。 How can I access remote database from local RStudio?如何从本地 RStudio 访问远程数据库?

Code in R: R 中的代码:

library(RPostgreSQL)
library(DBI)

drv <- dbDriver("PostgreSQL")

con <- dbConnect(drv, user='postgres', password='password', dbname='dvd', host='ec2-xx-xxx-xxxx-xxxx.ap-northeast-2.compute.amazonaws.com',port=5432)

dbListTables(con)

Result:结果:

Error in postgresqlNewConnection(drv, ...) : 
  RS-DBI driver: (could not connect postgres@ec2-xx-xxx-xxxx-xxxx.ap-northeast-2.compute.amazonaws.com:5432 on dbname "dvd": FATAL:  no pg_hba.conf entry for host "130.17.152.1”, user "postgres", database "dvd", SSL off
)
Calls: <Anonymous> ... eval -> dbConnect -> dbConnect -> postgresqlNewConnection
Execution halted

For AWS security group, I have added an inbound for PostgreSQL with its source set it to anywhere with port range 5432.对于 AWS 安全组,我为 PostgreSQL 添加了一个入站,其源设置为端口范围 5432 的任何位置。

By default Postgres does not allow remote connections.默认情况下 Postgres 不允许远程连接。 To allow remote connections you will need to add an entry to the pg_hba.conf file on the EC2 server.要允许远程连接,您需要在 EC2 服务器上的pg_hba.conf文件中添加一个条目。 The exact location of pg_hba.conf varies with different installations, but here are some paths to try: pg_hba.conf的确切位置因不同的安装而异,但这里有一些路径可以尝试:

/etc/postgresql/[VERSION]/main/
/var/lib/postgresql/[version]/

Once you've found your pg_hba.conf you need to add a line to allow remote connections, for example.例如,一旦您找到了pg_hba.conf ,您需要添加一行以允许远程连接。

# TYPE  DATABASE        USER            ADDRESS                 METHOD
  host  dvd             postgres        130.17.152.1/32         md5

This assumes that your local server IP (130.17.152.1) is static.这假设您的本地服务器 IP (130.17.152.1) 是 static。 If not, you could use 0.0.0.0/0 for ADDRESS, also, you can use all for DATABASE and USER like so:如果没有,您可以将 0.0.0.0/0 用于 ADDRESS,也可以将all用于 DATABASE 和 USER,如下所示:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
  host  all             all             0.0.0.0:0                     md5

Depends how tight you want your security to be, but bear in mind this is security at the database level and is completely separate to your AWS security groups.取决于您希望安全性有多严格,但请记住,这是数据库级别的安全性,与您的 AWS 安全组完全分开。

See https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html for more information.有关详细信息,请参阅https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html

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

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