How to switch my Rails app to postgresql from Sqlite3? - ruby-on-rails

So I started working on a Rails app recently and we decided (well not me, the person working on it with me) that we should switch from Sqlite3 to Postgresql. I've installed Postgresql on our server properly, created the databases for dev, prod, and test, and updated my Gemfile and database.yml files with the code for Postgres. The thing I'm unsure of now, is how to switch out all the files in the db directory with the Postgres databases. Do I just delete the contents of the db directory in my app and run rake db:create?

You'll want to edit config/database.yml to use postgresql instead of sqlite.
The migrations in db/migrate/*.rb are hopefully cross-database compatible, and wont need to be changed.
Running rake db:create db:migrate with the new database.yml should create the PostgreSQL database and you'll be up and running.
In reality, you'll probably run into various problems, but this will be a starting point.

Related

How to run migrations on a different database server through Heroku?

We need to add the credentials of a database in database.yml file under a different environment like remote_database:
remote_database:
adapter:
encoding:
username:
...
And after adding all this, running the following command from the local terminal does the job:
RAILS_ENV=remote_database rails db:migrate
I'm trying to accomplish the same thing on Heroku. I have pushed the changes in config/database.yml, and I'm trying to execute the following command:
RAILS_ENV=remote_database heroku run rake db:migrate
# or
heroku run rake db:migrate RAILS_ENV=remote_database
Seems like Heroku is completely ignoring RAILS_ENV or the settings for remote_database env in config/database.yml file. Heroku always makes the changes in the regular database server connected with it that can be found at DATABASE_URL.
Is there a way to run the migrations on a different database server through Heroku?
Heroku injects database.yml and overrides it completely with Rails under 4.1 version or overrides partially and allows a way for us to prevent overriding from Rails 4.1. Check the complete explanation about Rails database connection behaviour on Heroku article
So, in your case
If you are using Rails 4.1+: you may try to add url key to your database.yml as described in Active Record 4.1+ Escape Valve section of above link.
If you are using Rails under 4.1 version: override database connection by an initializer. See Heroku article ("Otherwise if you are using an older version of Rails you will need to use an initializer" section)

I cannot deploy my newly created app on heroku because of postgresql

I'm learning Ruby on Rails, and having lots of difficulty setting up.
So I have created this new app, using 'rails new' command.
Modified a bit, and now on way to deploy it on heroku.
First try, it failed because heroku required the app to use postgresql rather than sqlite3, which was set as a default database.
So I downloaded and installed postgresql, and here is the where the trouble began.
After installing the postgres, I set up the path.
And then I was unable to run the app locally anymore.
In command, "rails server" command is not working anymore, showing all kinds of error not being able to load stuff.
It looks like this:
D:\ruby\appname>rails server
c:\RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/pg-0.17.1-x86-mingw32/lib/pg.rb:10:in 'require': cannot load such file --2.1/pg_ext <LoadError>
and another bunch of load error.
No need to mention deploying on heroku, it fails everytime.
After installing postgresql, I can't use any database related commands as it says "loaderror" everywhere.
I can't run the app locally, nor deploy on heroku.
How should I fix this database problem?
In development you can still use SQLite, and in production use Postgres.
It's not advisable forever to have that disparity, but it does mean you an make progress.
Remove any production settings in database.yml, but still include 'pg' in your Gemfile.
Heroku will automatically insert database settings for Postgres in production. Deploy and then run heroku run rake db:migrate to set up your database.

sqlite3 won't go away....ruby on rails

I am trying to get rid of sqlite3 on my rails app and use postgres instead. I updated my gem file and removed it. manually uninstalled the sqlite3 gem. Got rid of it in my database.yml file. When I run rake db:create (As instructed on heroku) I get this:
db/development.sqlite3 already exists
I tried a whole bunch of rake commands and it continually refers to sqlite3. Why? I removed it? How do I remove it permanently from my app so I can go ahead and use postgres to deploy to heroku?
thank you so much itches head in mild frustration
This might be a sticky setting because spring is running. Did you try stopping spring?
spring stop

Error when creating database record on rails console

I'm trying to create a new record through the rails console but I'm getting the following error:
PG::ConnectionBad: FATAL: database "my_database_development" does not exist
I've recently changed from Sqlite3 to PG to be able to deploy to Heroku. Is this what is giving the error?
Thanks a lot!
It looks like you have not yet run
rake db:create
This will "create" the databases on your PostgreSQL server. A step that you didn't have to do with SQLite, but Postgres requires it. As TK-421 said, make sure that your database.yml is configured for your OS and Postgres, not SQLite.
Here is a (possibly outdated) Railscast on the topic.
http://railscasts.com/episodes/342-migrating-to-postgresql
Is this running locally, or on Heroku? In config/database.yml, the local user you specify will need to have CREATEDB privileges (as a superuser, for example).
There's a little more configuration required in this case than for SQLite. Google will show you a bunch of tutorials specific to your OS.
If you see this problem in production on heroku, start the console like this:
rails console production
If you have that problem in development on your machine, check your config/database.yml. It is points to a local development database that does not exist.

Migrate Data and schema from development to production rails heroku

I have facing problems with migrating data to my heroku app which has Postgresql as database for my hosted site(Production). At my development site i have rails 3.2.13 with Sqlite3 as database. I have followed Ruby on rails Tutorial by Michael Hartl
i have used git push heroku to update my site at heroku. i also want to update database along with data. But heroku run rake db:migrate migrates schema not data. I tried db:push to push data to heroku but i get error
dependency.rb in 'to_specs' :Could not find sequel (~) 3.20.0
also i have searched and found that i should first my sqlite data to dump.sql and then run
heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql as answered in https://stackoverflow.com/questions/15371394/...
but it failed with
the local psql command could not be located
please tell me what i am doing wrong. or what is the right way to update heroku postgresql with my development sqlite3 data.
Thanks in Advance
It is not a good idea to fill the production database with the data that you have now in the developement database. Because, if you have problems with your production database in the future, and you need to refill it again, your development db may changed (e.g dropped), and you are not going to be able to do it again.
For this need, Rails provides seeds in db/seeds.rb file. You should create all the neccessary objects there.
Then when you push your code to Heroku, Heroku is going to prepare the database, create the schema, and seed it. If you need to seed the db manually, you can run bundle exec rake db:seed, if you want to run it in Heroku: heroku run bundle exec rake db:migrate

Resources