简体   繁体   中英

How to securely connect to PostgreSQL database, with Ruby?

I feel uncomfortable saving my password in a file:

require 'pg'
conn = PG::Connection.open(host: 'server.example.com', password: 'hello_everyone')

Also, is there a way to determine or ensure that the transmission is encrypted? I am just worried about the implications of running my app locally, when it must connect to a remote database (I am worried about all the data, including the authentication credentials, being sent in the clear).

Regarding the password :

I would recommend setting it in an ENV variable. Take a look at dotenv gem . Basically, you are going to be able to do something like:

require 'pg'
conn = PG::Connection.open(host: ENV['database_host'], password: ENV['database_password'])

The values that are loaded into ENV will be stored in a file ( .env ) which you should not commit.

Regarding data encryption

You should take a look at SSH tunneling to connect to the remote DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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