简体   繁体   English

Postgres <=> Rails。 如何从database.yml自动创建用户

[英]Postgres <=> Rails . How to auto-create a user from database.yml

I am using PG for my rails4 application. 我正在为rails4应用程序使用PG。

Every time I git clone to a new machine or clear my project, I have to keep creating the user for postgres before I get the app working again. 每次将git克隆到新机器上或清除我的项目时,我都必须继续为postgres创建用户,然后才能使应用程序再次运行。

This is a problem when I share my code with designers etc and they have to unnecessarily get into details of setting up the db users and making them super users etc. 当我与设计者等共享我的代码时,这是一个问题,他们必须不必要地详细了解设置数据库用户并使其成为超级用户等的细节。

Is there a way I can automate this , given that I am using the PG gem and the Rails already know about the users I need from my database.yml 鉴于我正在使用PG gem,并且Rails已经从我的数据库中知道了我需要的用户,有没有办法使它自动化。

You shouldn't be doing this. 您不应该这样做。 database.yml doesn't belong in version control, see How to manage Rails database.yml . database.yml不属于版本控制,请参阅如何管理Rails database.yml

You should be storing a template file (I use config/database.yml.example ) and each person who clones your app should copy the template file to config/database.yml , and add to it their existing database user and password, for connecting to their local development instance of Postgres. 您应该存储一个模板文件(我使用config/database.yml.example ),每个克隆您的应用程序的人都应将模板文件复制到config/database.yml ,并将其现有的数据库用户和密码添加到该文件中以进行连接到他们在Postgres的本地开发实例。

Your designers should also have a single development user on their local machines, and it's up to them to place their credentials into config/database.yml . 您的设计师还应该在本地计算机上只有一个开发用户,并且要由他们将凭据放入config/database.yml

There is no way to have Rails create database users for you, because it's supposed to be your job to tell Rails existing database user it should use. 没有办法让Rails为您创建数据库用户,因为告诉您应该使用的Rails 现有数据库用户是您的工作。 All your apps, in your development environment, should be using the same user. 开发环境中的所有应用程序都应使用同一用户。

If you run the initdb command with the --auth-local trust option right after PostgreSQL installation, it will bypass all future database user authentication: 如果在安装PostgreSQL后--auth-local trust使用--auth-local trust选项运行initdb命令,它将绕过所有将来的数据库用户身份验证:

initdb /usr/local/var/postgres -E utf8 --auth-local trust

Of course this would be a major security issue when used outside a development environment. 当然,在开发环境之外使用时,这将是一个主要的安全问题。 But since you mentioned the specific need to have a quicker setup for people who work on the project, it seemed like it might be a possible approach in your case. 但是,由于您提到了为项目人员快速设置的特定需求,因此,在您的情况下,这似乎是一种可行的方法。

@meagar is correct about keeping your passwords and keys out of version control, so definitely keep that advice in mind. @meagar关于使您的密码和密钥不受版本控制是正确的,因此请务必牢记该建议。

It's conventional these days to use environment variables to store login information, because that data is inherently tied to the environment you're running on. 如今,通常使用环境变量来存储登录信息,因为该数据固有地与您所运行的环境相关。 So you could, in fact, check database.yml into version control, provided you put in calls to environment variables, rather than the actual secrets. 因此,实际上,只要您调用环境变量而不是实际机密,就可以将database.yml检入版本控制。

For example, you could put something like username: <%= ENV[DB_USER_DEV] %> and password: <%= ENV[DB_PASS_DEV] %> in your database.yml file. 例如,您可以在database.yml文件中username: <%= ENV[DB_USER_DEV] %>password: <%= ENV[DB_PASS_DEV] %> Then, put DB_USER_DEV=my_psql_username and DB_PASS_DEV=hashed_gibberish into ~/.profile . 然后,将DB_USER_DEV=my_psql_usernameDB_PASS_DEV=hashed_gibberish放入~/.profile

With vagrant, I create the user and environment variables in the provisioning of the Vagrantfile for the base box. 使用vagrant,我在为基础框提供Vagrantfile时创建用户和环境变量。 All our projects use the same base box, and adding those ENV vars to database.yml more or less automate everything in dev! 我们所有的项目都使用相同的基本框,并将这些ENV变量添加到database.yml或多或少地自动化了开发中的一切!

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

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