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.
Related
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
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
I am new in ror developement..i was working on a LIVE server...I just uploaded a file through sftp...after 1 day server suddenly stopped working...You can see the error message from here
it shows
There appears to be a database problem.
Your config/database.yml may not be written correctly. Please check it and fix any errors.
Your database schema may be out of date or nonexistant. Please run rake db:migrate to ensure that the database schema is up-to-date.
The database server may not be running. Please check whether it's running, and start it if it isn't.
Looking at the error page you seem to be using Rails 2.3?
At a guess you have a MySQL database not an SQLite running. You should have the user name and password for the database around somewhere (replace the relevant fields in the 3 sections with them).
Change the database names to reflect your database names.
The server admins might have set a specific socket for MySQL in which case replace the '/tmp/mysql.sock' with the socket number.
Check your Gems to see if the MySQL adapter is installed (you appear to be using Rails 2.3 so try gem list on the terminal for your server - make sure that you are in the root directory for the app).
If the MySQL gem is missing use gem install to install it (this will depend on what your hosting provider allows).
The following links are pretty old - targetted towards Rails 2 which you appear to be using.
http://www.ruby-forum.com/topic/139710
http://forums.mysql.com/read.php?116,353922,359544
database.yml
development:
adapter: mysql
encoding: utf8
database: temp_development
username: root
password:
socket: /tmp/mysql.sock
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
encoding: utf8
database: temp_test
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
database: temp_production
username: root
password:
socket: /tmp/mysql.sock
So I am developing locally using Rails 3.2 and mysql. My local machine is a Mac and my database.yml for development is:
development:
adapter: mysql2
database: dbname
encoding: utf8
host: localhost
port: 3306
timeout: 5000
socket: /tmp/mysql.sock
And for test it's
test:
adapter: mysql2
database: dbname
encoding: utf8
host: localhost
port: 3306
timeout: 5000
socket: /var/lib/mysql/mysql.sock
Test and production servers are on CentOS and the socket works correctly when deploying to them. However I just went to do a manual rake and got the
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My site works, but I'm curious if I'm should be handling database.yml separately for deployment since it's somehow looking at development when I run rake?
Looked for a suggestion and didn't see the same issue, apologize in advance if I missed it.
You can specify the Rails environment when you run the Rake task.
rake db:migrate RAILS_ENV=production
I can think of three alternatives:
Change your database.yml and just don't version in the changes
Use capistrano and its shared folder to handle different database.yml
Use an environment variable i.e. ENV['TEST_SOCKET']
Just setup a new rails 3.1.3 app using mysql (mysql2 gem) on CentOS 5 server / apache / passenger... I have correctly setup a database and a user for that database and I have added the login and info into my database.yml file... I can generate stuff, and rake db:migrate ok but the "We're sorry, but something went wrong." message is being rendered in the browser and this message is showing up in my production.log file!
Started GET "/" for xx.xxx.xx.xxx at 2011-12-29 19:52:35 -0600
Mysql2::Error (Access denied for user 'root'#'localhost' (using password: NO)):
weird, I am not using "root" as the login info in database.yml... Any suggestions?
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: the_db_I_made
pool: 5
username: the_user_I_made
password: the_password
socket: /var/lib/mysql/mysql.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: the_db_I_made
pool: 5
username: the_user_I_made
password: the_password
socket: /var/lib/mysql/mysql.sock
Bunch of questions / suggestions:
Can you connect to the database using the terminal?
mysql -u root -p
Also, have you tried this on development mode? If so, please share the results.
Try removing
socket: /var/lib/mysql/mysql.sock
Is the gem installation correct?
gem check mysql2
EDIT:
There is a lot of difference in development and production mode ( including but not limited to, environment variables like the database connection string, asset pre-compilation, different level of logging, custom debug info on error pages )
** Embarrassing!!**
You have not included the "host" property in your config!
Try this:
production:
adapter: mysql2
encoding: utf8
reconnect: false
host: your_host #<----- normally localhost
database: the_db_I_made
pool: 5
username: the_user_I_made
password: the_password
socket: /var/lib/mysql/mysql.sock
Ok, I guess this is all part of my learning curve :)
What it ended up being was this: The gem for passenger that I installed originally was version 3.0.9, I did a gem update --system and that installed version 3.0.11. So the server thought it was using version 3.0.11 and I was still pointing to libraries for 3.0.9 in my rails_server.conf file. I found how to upgrade on http://blog.phusion.nl/
In Ubuntu 1)/usr/bin/mysql_secure_installation 2) Run in Terminal
follow the steps carefully change the mysql password
here.Everything done .then go to database.yml change the password
previously what u have created. Run rails server and check the
application .. it will works
Did you create the database "the_db_I_made"?
First create it, then only run it, else it will show this error continuously.
Now try to tun it.
For Me It was giving error because of no database. So i created one Using :
$ rake db:create
And the problem was solved :D