So, had a working postgres db instance with my rails app on my development Mac, but the migrations were getting unmanageable as I'm building the model up from scratch (i.e. adding, removing, changing columns), so I decided to start clean. After running rake db:drop, I can not recreate via rake db:create. I simply get the following:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"xyz_development", "pool"=>5, "username"=>"xyz", "password"=>nil}
I am thinking I need to create the user in postgres, but shouldn't it be there from initial setup?
Thanks.
You should see the SQL statement to create the database in the log output, and probably also the error. Check that.
Also check that the database doesn't already exist. Try creating the database manually using psql(1) with the username/password you use in rails and see what happens.
$ createdb xyz_development
Use the postgres bin createdb
Related
I am a completely new at this; I used the psql console to create tables for the db, but I think I'm doing it wrong since when I run rake db:migrate, everything gets wiped. So my question is how do I get it to stop dropping all my tables upon a reset without typing all my tables again? I think I'm supposed to use models, is this correct?
EDIT - So I have wasted all my time making tables using the psql console?
You need to create tables "the Rails way" with migrations. You should read the link provided, but essentially you create migrations to track the changes in your database schema over time. You have migrations for creating tables, adding new columns, changing column definitions, etc. You can generate a migration to create a table from the command line like so:
rails generate model Fruit title:string amount:integer
rake db:migrate
This will not only create the table fruits with the specified name, it will also create a model Fruit with which you can query the table. You're also free to edit the migration created before running rake db:migrate.
You should create migration files to build your database. That is what you are doing when you run rake db:migrate: you are taking your migration files and applying them to your database. Look at this guide for more information about Rails migrations.
By running $ rake -T you can check every rake task. Everything is being wiped because other tasks are run along with certain rake tasks. Try
$ rake db:drop
$ rake db:create
$ rake db:migrate
Your database should be created properly, as long as you gave database creation permissions to your Postgres role.
I was trying to create a rails project with postgresql
I use this
>>rails new ReadingList --database=postgresql
>>rails s
However, it shows this error
Then, I jump to psql and create readinglist_development by myself, but it still show the same error.
In Rails practice, run rake db:create to create the database after creating new Rails project.
I believe the name of the database(readinglist_development) is not matching the database name of ReadingList_development as in database.yml which Rails does not recognize the database created using psql.
Its better not to create databases form db workbenches, better to use rails db commands.
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.
I am having a lot of issues with rails migration mechanism.
I think I have run a migration file and has been executed partially.
So when I am trying to run
rake db:migrate
again it gives me an error the column name already exist.
I am trying to reset it with
rake db:reset
and gives me an error
Unknown database 'databasename'
Is there a way to reset the whole mechanism ?
Is it a good idea to manually drop all tables and try to run rake db:migrate again ?
You should change the database name in your database.yml.
When I tried to switching from sqlite3 to postgresql in my existing application I faced this problem with rake db:migrate, I did the following
1 - rake db:create
2- rake db:migrate I got this error:
== AddColumn1: migrating ===================================================== -- add_column(:users, :first_name, :string) rake aborted! An error has occurred, this and all later migrations canceled: PG::Error: ERROR: relation "users" does not exist
3- rake db:reset
4- rake db:migrate , now migration is done with no errors
I have lost my data specially my admin user because of rake db:reset and my questions is :
1- why I forced to use rake db:reset ?
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
So, what you are saying is you lost data in your sqlite3 database? This should not have happened. Did you change your database.yml in config folder to point your app to the new Postgres DB? If you did point it to the new DB, you should not have lost any data in sqlite3 DB since the app is not hitting it any more.
If you have indeed changed your db config to use the new db, you may not have lost any data. I hope this is the case. :)
1- why I forced to use rake db:reset ?
I don't think you had to run rake db:reset on your new DB, unless your migration files are messed up.
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
You can create a rake task or a script that points to two db instances (the old one and the new one you are migrating to). You can then pull data from the old db and then massage data as needed (each RDBM has their own minor syntactical differences) and the load them to the new db.
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
It depends how you set up your Postgres DB instance you created...
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
Setting up your DB on Heroku would be very simple. All you have to do is the following:
heroku crate your-app-name
git push heroku master
heroku run db:migrate
But, I think you have some issues in your migration files. Nevertheless, you can actually get a username/password for your DB on Heroku and then use your script to push the data over to it from your local DB.
I hope this helps!
If you are using Devise, you can reload your user account information using:
rake db:seed
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