Two Terminal sessions with different results - ruby-on-rails

I'm setting up a new computer to use an existing Rails application (Rails 3.2 with Postgres). I installed postgres via homebrew, along with the Lunchy gem as documented here: http://www.moncefbelyamani.com/how-to-install-postgresql-on-a-mac-with-homebrew-and-lunchy/. No problems getting a user account and starting/running my application. I ran rake db:create:all to create all of the databases. The site works and loads from only one Terminal tab.
When I open a new tab, same location, and try to start the server, I get the following error:
Users/jpetrarca/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1216:in `initialize': could not connect to server: No such file or directory (PG::Error)
19:24:39 web.1 | Is the server running locally and accepting
19:24:39 web.1 | connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I don't know why one terminal session would work and another not.
Interestingly, I can start/stop postgres with lunchy from the non-working terminal and it does, in fact, use the same service and turns off the db being accessed from the working terminal (which is expected).
I initially found this problem while trying to run my rspec tests and connect to the test db.

Related

rails database setup command can not make the database as I wish?

I am newbie with Ruby Rails and here is my problem.
I created a very simple Rails program by this command
rails new freelancer --database=postgresql --javascript=webpack
And I had no error
In this file
config -> locales -> database.yml
I saw 2 files
database: freelancer_test
database: freelancer_development
Then I ran this command
rails db:setup
It showed me this error
Is the server running locally and accepting connections on that socket? Couldn't create 'freelancer_development' database. Please check your configuration. rails aborted! ActiveRecord::ConnectionNotEstablished: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? Caused by: PG::ConnectionBad: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket? Tasks: TOP => db:setup => db:create (See full trace by running task with --trace)
I thought that it must created 2 databases freelancer_test and freelancer_development ?
Could you please give me some advices ? Thank you in advance.
A Postgresql server is required and configured properly to run this Rails application. From the error it expects a postgres server running locally. But you can also connect to a standalone DB server remotely.
Your database.yml should contain correct connection info then run the same command again.
It means that you have no PostgreSQL instance running locally.
Two choices:
Either you install it, you can follow this nice tutorial: https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-macos
Or you can just use SQLite for you dev environment
Unless you're really need PostgreSQL features for your development, I'd go for the second option, as you're beginner with RoR, you probably want to focus on the development of your app first. Using the default sqlite database requires no dependencies as the database is a text file in the end.
You can create the app with the following command:
rails new freelancer --javascript=webpack

PostgreSQL error: could not connect to database template1: could not connect to server:

I'm running into the following error after uninstalling and installing several times I just can't seem to get postgresql working. I am trying to setup the coding environment for ruby on rails 6 on windows 10. I have followed the following articles and have made it all the way to creating a user where i get the following errors.
links to how i got here
https://github.com/serivas/wsl2_rails_setup
https://gorails.com/setup/windows/10#ruby-rbenv
https://www.postgresql.org/download/linux/ubuntu/
Error I'm getting when i type
sudo -u postgres createuser abc -s
Error Message;
createuser: error: could not connect to database template1: 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"?
Thanks in advance!
Here's how I solved my problem for anyone that might be having similar issues.
Make sure that your windows 10 is updated to be able to run WSL2
First follow the link below to get Ruby and Rails installed.
https://gorails.com/setup/windows/10
Setup PostgreSQL from the link above,
the link below also help me setup PostgreSQL;
https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database
If your having trouble logging into Postgres make sure that you create a user in Postgres that allows the user to be able to CREATEDB. So if your user is JohnDoe make sure that you give permission for JohnDoe to create database.
Links to help you out with granting permission.
https://dba.stackexchange.com/questions/33285/granting-a-user-account-permission-to-create-databases-in-postgresql
Make sure that you are starting your postgresql server.
sudo service postgresql start
And to Stop
sudo service postgresql stop
After User has rights to create the database, then you can go ahead and type in
rake db:create

Ruby On Rails - "OpenSSL::Cipher::CipherError at ..." when connecting to imported database

I am running my Ruby On Rails 5 app on localhost and now, I imported the production database. It's a PostgreSQL database, exported via the pg_dump tool.
When I modified the database.yml file the Rails app and set there the newly created database, I got this error when running the Rails app (tried also to change the port on which the app is running, but it didn't help):
OpenSSL::Cipher::CipherError at /
In the Rails console is not any information about the error.
What is the reason of such an error? I tried to export the database from the staging server and use it on localhost and everything worked fine.
Based on the docs: https://ruby-doc.org/stdlib-2.4.1/libdoc/openssl/rdoc/OpenSSL/Cipher.html#method-i-final
It looks like you don't have the proper encryption key to connect to the production db.
I would guess you either do have the proper one for staging, or staging is running unencrypted.
Here's someone else with the same error caused by an incorrect key: OpenSSL::Cipher::CipherError when running staging DB on local

Error while typing rails server command

I am a beginner.I am trying to launch rails server using the command.But I am getting an error. I tried searching in the google but no results.I will attach a picture of the log I got when I executed that command.
What Mysql error are you getting?
And what is in your config/database.yml?
So far, you have created the folder structure for a Rails app (rails new), then installed all the relevant components (bundle install).
When you start the server (rails server) it starts in "development" mode (you also have "test" mode for unit tests and "production" mode for when your app is live - and each has slightly different options). One of the first things the Rails server tries to do is connect to the database, so it looks in config/database.yml for the database specified in the development section.
So probably, it's trying to connect to a database that doesn't exist yet, with a username and password that are wrong.
First thing to do is to update the username and password in config/database.yml to match your local Mysql server.
Second thing to do is to build the development database; the command for that is "bin/rake db:create" (or "bundle exec rake db:create" if you're on Rails 3.x).
Hopefully that should be enough to get your server started.
How did you setup your rails app?
It seems like maybe you didn't type
bundle install
This command downloads and updates all of your gems. Action View is a rails dependency.

Disable MongoDB from rails 3.0.9 application start up?

My project uses both MySQL(for ActiveAdmin tables) and MongoDB(for rake task that does logs file parsing) as database. I have used Mongoid to connect my app to MongoDB. So I just have mongoid.yml file extra in /config directory along with other usual files(No other files to setup MongoDB connection). However whenever I try to run migrations for MySQL or start my app it requires MongoDB running in the background else the server/task wont start and gives an error
Failed to connect to a master node at localhost:27017 (Mongo::ConnectionFailure)
Keeping MongoDB continuously running is not required. Please guide me in excluding this compulsion.
There seems to be some discussion on this topic here: https://github.com/mongoid/mongoid/issues/1166
Perhaps you could wrap the connection so that it checks for a certain flag or environment variable which you would use for mysql only mode.

Resources