Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?
I'm having trouble understanding how Heroku, Rails and PG gem work together.
It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?
Below are the steps to access Heroku db from local development:
Login to your heroku account.
Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url
Select your application database.
You will able to see your heroku pg credentials:
Host xxxxxxxxx.77777.amazonaws.com
Database 42feddfddeee
Username 44444444444
Port xxxx
Password 777sfsadferwefsdferwefsdf
collect above details and put in your databse.yml file development evn:
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Restart you application,
Best of luck..!!
I am not a postgres user but this should work.
You can alter your database.yml to include host and port
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
host: heroku.host.name
port: <postgres listen port>
And of course, you should allow connection on the server side, both at the firewall level and database level.
A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI
DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect # or what ever method you want to print it
I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.
From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate
From my config/dtabase.yml
development:
adapter: postgresql
encoding: unicode
database: di_development
pool: 5
username: davidlee
password:
test:
adapter: postgresql
encoding: unicode
database: di_test
pool: 5
username: davidlee
password:
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
and it works. I can't remember doing anything else.
Pierre
Related
An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?
For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy
So I have a standard database, then a database that I tie into with readonly permissions to access blogs from a third party blogging platform(ghost).
My database.yml looks like this:
staging:
adapter: mysql2
encoding: utf8
database: application
username: application
password: XXXXXXXX
host: 127.0.0.1
port: 3306
staging_blog:
adapter: mysql2
encoding: utf8
database: application_blog
username: application
password: XXXXXXXX
host: 127.0.0.1
port: 3306
The migrations seem to run without error when run with the 'staging' environment, but they obviously aren't running against the blog database, as that user only has readonly access to the application_blog database.
In my unicorn.stderr.log, I get the following output:
ActiveRecord::AdapterNotSpecified: 'application_blog' database is not configured. Available: ["staging", "staging_blog"]
So I guess my question is... what is necessitating the application blog to be configured in some fashion? How do I make it so that this blog can have a completely different db schema than the staging connection.
It seems you have not created the database 'application_blog'.
I'm new to Ruby on rails. I created my rails project and I would like to connect to an existing postgresql database (of company I work for) and display then some data in my web app.
Can anybody help out how to do that?
These directions assume you are using some version of Linux. However, they would be very similar on other operating systems.
Add the 'postgresql' gem to your Gemfile:
gem 'pg'
Then open a terminal window in the root directory of your application and run:
bundle install
Edit postgresql.conf (located on the remote postgresql server) and find the line that reads:
#listen_addresses = 'localhost'
Remove the comment and change it to:
listen_addresses = '192.168.0.14, localhost'
Replace '192.168.0.14' with the ip of your Rails application.
Now open pg_hba.conf (located on the remote postgresql server) and scroll down to:
# Put your actual configuration here
Directly below that enter your configuration like so:
# TYPE DATABASE USER ADDRESS METHOD
local all all localhost md5
host all your_user 192.168.0.14 md5
After saving both of those files run the command:
sudo service postgresql restart
Now edit your Rails application's config/database.yml:
production:
adapter: postgresql
encoding: utf8
database: the_database_name
username: your_user
password: your_database_password
host: 192.168.0.14
port: 5432
pool: 10
development:
adapter: postgresql
encoding: utf8
database: the_database_name
username: your_user
password: your_database_password
host: 192.168.0.14
port: 5432
pool: 10
Change 'the_database_name', 'your_user', and 'your_database_password' to the appropriate values.
After that, you should be good.
I have a rails 4 app on heroku with a PSQL database.
This has all been fine in development, but I'm coming to my first attempt at publishing the app with content in the database.
I recently reset the database locally and in heroku. And also did:
heroku ran rake:db migrate
I then created the first article in my articles resource and committed, pushed and pushed to heroku. I expected to see that article displayed (as it is in my local host).
However, when I go on local host in production mode or to the published site, the article is not there.
Is there a step I'm missing for how to push to heroku with the db content you want? Is there something that needs to be done to make the production database recognise the development database? Not sure about the steps for publishing the production db.
In my database.yml file I have:
staging:
adapter: postgresql
encoding: unicode
database: vh_staging
pool: 5
#username: vh
#host: localhost
development:
adapter: postgresql
encoding: unicode
database: vh_development
pool: 5
#username: vh
test: &test
adapter: postgresql
encoding: unicode
database: vh_test
pool: 5
#username: vh
production:
adapter: postgresql
encoding: unicode
database: vh_production
pool: 5
#username: vh
You push on heroku only your code, not a database data.
Try doing that by seed:
heroku run rake db:push
heroku pg:reset DATABASE
heroku run rake db:seed
You should have your data as ruby code in your the file db/seeds.ru to do that.
I have a Rails app that has been using sqlite3 for the DB. Deployed to Heroku. Then find out that Heroku recommends switching to PostgreSQL. So now I'm trying to switch over without any luck. Was trying to use this Railscasts video for help.
I installed Homebrew. Installed PostgreSQL via Homebrew. After installation, there was no mention of creating a username or password for postgres.
I edited my Gemfile to
gem 'pg'
for both production and development and did bundle install.
I edited my database.yml file to this:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
Like Ryan says to do in the video, I try this command:
rake db:create:all
Only to get this error:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I do some more searching, and see that some tutorials show username and password included in the database.yml file. I then find out how to setup a user for Postgresql
After entering in the command $ createuser joe, I was never given the options that the docs say you'll be asked. Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either.
So, I'm assuming, after creating the user "joe", I reedited my database.yml file to include the user field I just created:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
Only to still get the same error of not being able to connect.
I've ran the command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running as well.
Just in case it's needed, when I run
which psql
I receive this:
/usr/local/bin/psql
Is there something that I'm missing? The "database name" part of the database.yml file. This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all command? I'm assuming the latter, so the name of the database doesn't matter?
Lazy option: Add host: localhost to your database.yml.
Only slightly less lazy option: Uninstall and reinstall the pg gem.
What's going on here? There are a number of ways for Postgres to already exist on your system, and the pg gem will use their pg_config output and build for their needs if you install the gem before installing your own copy of Postgres.
In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket.
when you are installing Postgres via Homebrew it will add your username to postgres with an empty password.
Just open a command prompt and type
whoami
Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password.
Maybe add host: localhost
to you database.yml