How to Use Mongoid on Rails? - ruby-on-rails

I've been trying to use Mongoid with Rails on Ubuntu. I have installed mongoDB via apt-get. But when I try to run the rails server, I get this error:
/home/myusername/.rvm/gems/ruby-1.9.2-p180#mysite/gems/mongo-1.3.1/lib/mongo/connection.rb:518:in
`connect': Failed to connect to a
master node at localhost:27017
(Mongo::ConnectionFailure)
I am just new to using Mongoid, any help will be appreciated. Thanks a lot in advance!

As Piotr says you need to start the Mongo DB server. If you want to quickly test this, run mongod from a shell.

You need to start MongoDB server. I don't know exactly how to manage services in Ubuntu, but somewhere in /etc/rc.d or /etc/init.d you should have the mongodb script, run it with 'start' argument. I think the default configuration should do for your setup, but check if you have config/mongoid.yml in your Rails app. It should contain something like:
development:
host: localhost
database: app_development
test:
host: localhost
database: app_test
# set these environment variables on your prod server
production:
[...]

Related

Rails development environment trying to connect to AWS database, not localhost's PostgreSQL

I have an existing Rails app which was previously running successfully on my local laptop (running OSX) with the database stored locally in PostgreSQL. I've also successfully deployed the Rails app to Elastic Beanstalk with the database in RDS - the hosted site is still running.
I haven't touched the localhost version for months and tried to start it today using rails server -e development, but when visiting http://localhost:3000/ as per usual, I get a PG error message
PG::ConnectionBad at / FATAL: password authentication failed for user
"murjfphxxxxxx" FATAL: no pg_hba.conf entry for host "115.x.x.x",
user "murjfphxxxxxx", database "d37vsvehxxxxxx", SSL off
The same error is thrown in the Rails console. It seems that the development version is trying to connect to the AWS database, rather than the localhost database, but I can't figure out why.
/config/database.yml lists the development environment as:
development:
adapter: postgresql
encoding: unicode
database: <my-app>_development
host: localhost
pool: 5
username: postgres
password: <password>
I can connect to the localhost PG database via PSQL and see the contents, so it appears that the localhost PG database is valid.
Why would the development environment try to connect to the AWS version of the database? Please let me know if you need any further information to debug this.
Using ruby-2.3.1, Rails 4.2.0, PostgreSQL 9.6.0.0
You probably have DATABASE_URL in your env. You can check by typing env | grep DATABASE_URL in terminal.
If so, try unsetting it with unset DATABASE_URL.
As said in documentation:
If you have both config/database.yml and ENV['DATABASE_URL'] set then
Rails will merge the configuration together. To better understand this
we must see some examples.
When duplicate connection information is provided the environment
variable will take precedence.

PostgreSQL rake db:create Error in local environment

I continue to run into issues whenever I use postgreSQL for Rails app development in my local environment.
I'm running on Mac OS X 10.7. I'm aware this version comes with a pre-installed Postgres version.
I've initially installed Postgres using the brew method and have, at least a few times, found success. I'm aware of the user authentication issues.
When punching in psql --version or which psql to determine which installation is being used, I've had to adjust my .bash_profile before to reflect the appropriate path with export PATH=/usr/local/bin:$PATH.
The issue I'm running into now, and hours among hours of Google haven't solved this, is that my machine has restarted without properly closing PostgreSQL. In turn, I am getting the following error when I try to execute rake db:create on a new application, I am getting the following error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"app_development", "pool"=>5, "username"=>"tomgeoco", "password"=>nil}
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
My config/database.yml looks like:
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
test:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
production:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
When I check the status of Postgres servers with ps auxwww | grep postgre I get the following:
tomgeoco 12596 0.0 0.0 2434892 408 s000 R+ 11:54AM 0:00.00 grep postgres
So what could the problem be in this scenario?
This issue is not about rails but postgres. I faced with exactly the same problem recently with django project.
According to your ps output postgres isn't working at the moment.
First you have to check logs, my one is /usr/local/var/postgres/server.log. It'll give you an idea about what's wrong on postgre starting up.
My problem was solved by fixing PATH variable in .bash_profile.
Try to start your postgre server manually checking all its output. If everything goes fine but broken on system reboot, check out your launchctl daemons to find out which config file is used. This might be a problem as well.
Here you could find good instructions of clear installing of brew version of postgres. And here is modifying of postgres default settings which might be usefull as well.

rails 3 postgreSQL basic 'database does not exist'

OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword

ROR change application database from SQLite to PostgreSQL

I have a web application which uses SQLite. I deploy it on heroku which uses PostgreSLQ. This causes problems sometimes and I was advised to develop my app using PostgreSQL instead of SQLite.
I found out that I should modify database.yml like that (same for test and production):
development:
adapter: postgresql
database: my_database
username: my_username
password: my_passwod
host: /var/run/postgresql or localhost
Well the only database I've ever used is SQLite, so I just tried to take my chances, but failed. I filled this file with some random data.
rake db:migrate resulted in:
When I used host: localhost
> could not connect to server: Connection refused Is the server running
> on host "localhost" and accepting TCP/IP connections on port 5432?
When host: /var/run/postgresql
> could not connect to server: No such file or directory
> Is the server running locally and accepting connections on Unix domain socket
> "/var/run/postgresql/.s.PGSQL.5432"?
I suppose I should start PostgreSQL server first, but have no idea how to do this. Please give me a step by step answer how to move from a SQLite application to a working PostgreSQL application.
I would like to advise to you that you should download Postgresql including the PGADMIN itself which is easier to use than the psql terminal.
And I think when you download/install Postgresql from their official website... the package was complete already.
Upon installing, the postgresql will ask you a certain password that you will be using in accessing your postgresql server.
After the installation, open the PGADMIN and connect to the server. Enter your password (which you had declared during installation).
If you can't connect to the server, then edit the port. To do this, right click the server then go to properties... edit the port into something which is free. Example: 5433 and so on. It's up to you.
If everything's finally working... setup the correct config for your database.yml
This is important:
development:
adapter: postgresql
database: name_of_database_here
host: localhost
username: postgres
password: your_db_server_password_here
pool: 5
timeout: 5000
port: 5433
Okay from that config info above, specify the important parts. By default, your db server username is postgres and obviously your host is localhost because you are setting up under the development.
If your port is 5432 by default then just remove the port part.
Let's go to your gemfile.
In order for you to deploy your app in heroku. Use gem 'pg' instead of sqlite3.
If you have an existing sqlite3 database then put the gem inside the development group. In that case, Heroku will successfully bundle during git push heroku master process.
group :development do
gem 'sqlite3'
end
Your gem 'pg' can either go outside the groups or put it in your production group.
Important:
Before any deployment procedure, make sure that you can run the app locally (localhost). Then if everything's working... that's the time that you should organize the necessary stuffs appropriately.
If you wish to switch to Postgresql instead of sqlite3 after pushing the app to Heroku... you can do so by pgbackups add-on and pg_restore the dump file into your local postgresql db server.
That's it. Hope it helps.
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

Configuring Postgres in Ubuntu 9.1 Rails dev-box for Cucumber testing

I'm trying to install Postgres 8.4 for a Rails dev-box and I'm having a couple problems.
I installed postgres and pg-admin3 through apt-get.
Using the lastest Rails 2.3.5 and lastest Ruby 1.9.1
Now the configuration is bothering me. I found some documentation regarding setting up the user for the postgres user(which is the default admin user for postgres I guess), which I did.
sudo -u postgres psql postgres
\password postgres
Prior to that I was getting an error...
FATAL: Ident authentication failed for user
After handling that, I managed to create a database in pg-admin3.
Updated the auto-generated database.yml to look like this.
development:
adapter: postgresql
encoding: unicode
database: rails-box_development
pool: 5
username: postgres
password: ********
host: localhost
port: 5432
After this rake db:create works. If I remove the host and port it doesn't work.
My Question is.. What is the default configuration for those developers that use Rails and Postgres for development? You use that ident stuff or what? What are your steps if you are setting up a new box?
You need to adjust /etc/postgresql/8.4/main/pg_hba.conf (or 8.3 if you are running that). Look here:
http://www.postgresql.org/docs/8.3/static/auth-pg-hba-conf.html

Resources