2

I have multiple versions of PostgreSQL running on my machine (running Ubuntu Trusty).

$ service postgresql status
9.1/main (port 5433): online
9.5/main (port 5432): online

I am running several Ruby apps with ActiveRecord, that are each using different versions of Postgres, and I want to be able to specify the Postgres version in database.yml, something like this:

default: &default
  adapter: postgresql
  encoding: utf8
  version: 9.1
  # ... or, since this seems to be close to pg_wrapper's syntax:
  cluster: 9.1/main

What I don't want to have to do is specify port: 5433, since that's likely to be different for each version on each machine it's running on. Is there a way to do this?

3
  • Why keep database connection details in version control in the first place? IMO . gitignore it and have everyone fill it in with their own details or set DATABASE_URL in the environment. Commented Jul 21, 2016 at 8:37
  • Yeah, great, cos they're all sure to get all the details right. Commented Jul 21, 2016 at 8:38
  • They probably know their machines better, right? This cluster-thingy, as far as I know, is Debian-specific. Are you expecting everyone to be on Debian (or derivatives, like Ubuntu)? Also note that pg_wrapper only affects utilities that Rails doesn't use (probably with the exception of pg_dump for schema in SQL). Commented Jul 21, 2016 at 8:54

1 Answer 1

2

You can put environment variables in your database.yml using a bit of ERB:

default: &default
  adapter: postgresql
  encoding: utf8
  port: <%= ENV['POSTGRESQL_91_PORT'] %>

That way your database.yml files are the same on your various machines and you just need to set up some machine-specific environment settings.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmmm... But I want to have it set once, in the repository, so I don't have to keep setting it up on every machine.
I don't think you can, Rails isn't going to know how to figure out which versions of PostgreSQL are running and which ports they're using on any random machine; suppose you have an Ubuntu box, a Debian box, a couple macs, how do you expect Rails to know what is running where? Environment variables are the standard way of doing this sort of thing, most apps I build end up with a handful of environment variables to deal with the differences between different developer machines, staging servers, and production servers.
Well, like I mentioned, there's pg_wrapper. Any Postgres command line script can take the option --cluster=9.1/main and choose the right service. Why not ActiveRecord's adapter?
You're welcome to submit a patch to Rails if setting environment variables is too difficult to manage.
How about: port: <%= `service postgresql status`.match(/9\.1\/main \(port (\d+)\)/)[1] %>? (Then again, that's probably still Ubuntu-specific...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.