[英]Ubuntu and Rails PostgreSQL setup
I am on a Ubuntu 14.04 LTS machine. 我在Ubuntu 14.04 LTS机器上。
I found useful information on how to configure PostgreSQL in Ubuntu for Rails development at help.ubuntu.com , at Heroku and at digitalocean.com . 我在help.ubuntu.com , Heroku和digitalocean.com上找到了有关如何在Ubuntu中配置PostgreSQL进行Rails开发的有用信息。
Putting everything together, all the information seems to converge on the necessity of creating a database superuser with login name that match my Ubuntu user name with: 将所有内容放在一起,所有信息似乎都集中在创建登录名与我的Ubuntu用户名与以下内容匹配的数据库超级用户的必要性上:
sudo -u postgres createuser --superuser $USER
When time arrives to create a password for the new superuser with sudo -u postgres psql
, I am wondering if Rails can use PostgreSQL without setting the password, if this password can and should be different from my Ubuntu account password and also whether database.yml
could be a security concern when pushing to Git repository hosting web sites and to Heroku. 当时间到了,使用sudo -u postgres psql
为新的超级用户创建密码时,我想知道Rails是否可以在不设置密码的情况下使用PostgreSQL,该密码是否可以并且应该与我的Ubuntu帐户密码以及database.yml
.yml不同。推送到Git存储库托管网站和Heroku时可能会引起安全问题。
In database.yml
in fact is recorded exactly this kind of sensitive information. 实际上,在database.yml
中正是记录了这种敏感信息。
According to Heroku it is necessary "to export the DATABASE_URL environment variable for your app to connect to it when running locally", with: export DATABASE_URL=postgres:///$(whoami)
根据Heroku的说法,有必要“导出DATABASE_URL环境变量,以便您的应用在本地运行时连接到它”,其操作如下: export DATABASE_URL=postgres:///$(whoami)
Is that really necessary? 这真的有必要吗? At help.ubuntu.com and digitalocean.com this information is not reported. 在help.ubuntu.com和digitalocean.com上没有报告此信息。
Finally I am wondering whether the choice of installing PostgreSQL through the PostgreSQL apt repository would be safe enough or it would be preferable to install the LTS version of Ubuntu. 最后,我想知道通过PostgreSQL apt存储库安装PostgreSQL的选择是否足够安全,还是安装LTS版本的Ubuntu会更可取。
There are two ways for Rails to set the connection with a database: via config/database.yml
or via the environment variable ENV['DATABASE_URL']
. Rails通过两种方式设置与数据库的连接:通过config/database.yml
或通过环境变量ENV['DATABASE_URL']
。 See at guides.rubyonrails.org 参见guides.rubyonrails.org
By default $DATABASE_URL
is empty: 默认情况下, $DATABASE_URL
为空:
echo $DATABASE_URL
If posgresql is installed via PostgreSQL apt repository, in order for Rails to use the pg
gem it is also necessary to install the libpq-dev
package, otherwise bundle install
will fail. 如果posgresql是通过PostgreSQL apt仓库安装的,为了让Rails使用pg
gem,还必须安装libpq-dev
软件包,否则bundle install
将失败。 See Can't find the 'libpq-fe.h header when trying to install pg gem . 请参阅尝试安装pg gem时找不到'libpq-fe.h标头 。
From 'man createuser': 来自“ man createuser”:
createuser creates a new PostgreSQL user (or more precisely, a role). Only superusers and users with
CREATEROLE privilege can create new users, so createuser must be invoked by someone who can connect as a
superuser or a user with CREATEROLE privilege.
When postgresql is installed, it creates a user postgres
with role postgres
. 当安装PostgreSQL的,它会创建一个用户postgres
与角色postgres
。 It also creates a postgres
system account. 它还创建一个postgres
系统帐户。 So this is why createuser
should be run as postgres
user in order to connect to postgresql for the first time and add the user $USER
(current system user). 因此,这就是为什么createuser
应该以postgres
用户身份运行,以便首次连接到postgresql并添加用户$USER
(当前系统用户)。
It is not necessary to create a password for the new database user. 不必为新的数据库用户创建密码。 Most people add database.yml
to their .gitignore
file so it stays out of version control. 大多数人将database.yml
添加到他们的.gitignore
文件中,因此它不受版本控制。 It is also possible to use .pgpass
to keep sensitive information out of the *.yml file: see postgresql documentation . 也可以使用.pgpass
将敏感信息排除在* .yml文件之外:请参阅postgresql文档 。
It is possible to connect to postgresql only as a database user AND through an existing database. 可以仅以数据库用户身份并通过现有数据库连接到postgresql。 During installation from the postgresql apt repository, postgresql only creates the postgres
user and the postgres
database. 从postgresql apt仓库进行安装期间,postgresql仅创建postgres
用户和postgres
数据库。
The psql
command allows the current user to connect to the postgresql database named after the current user. psql
命令允许当前用户连接到以当前用户命名的postgresql数据库。 So, if the system user is 'dave' AND there is a 'dave' database it is possible for 'dave' to connect to the 'dave' database with command psql
with no options. 因此,如果系统用户是'dave'并且存在'dave'数据库,则'dave'可以使用不带任何选项的命令psql
连接到'dave'数据库。
If 'dave' is a database user but the database 'dave' was not created, for dave to connect to postgresql it is necessary to specify an existing database with: 如果'dave'是数据库用户,但未创建数据库'dave',则dave要连接到postgresql,必须使用以下命令指定现有数据库:
psql -d postgres
Alternatively, it is possible for dave to connect to postgresql executing the psql
command as the postgres
user with sudo
: 另外,戴夫有可能连接到以sudo
作为postgres
用户执行psql
命令的postgres
:
sudo -u postgres psql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.