I have been using sqlite for my project,
made alot of changes in migration files, I want to upload to heroku, but it does not support sqlite, so I decided to change it to pg.
I changed the database.yml file to the right values, I try to migrate the database, but I get tons of errors of migration, where I decided backthen to remove a column, but it doesn't exist anymore.
Is there anyhow to create a migration file out of the current fine working database in sqlite, that will be put in one migration file to be migrated in pg version ?
This is an example of the configuration of a test database, actually it can be production or development if you want:
test:
adapter: mysql2
database: redmine_test
host: localhost
username: 'user'
password: 'password'
port: 3306
reconnect: true
encoding: utf8
test_sqlite3:
adapter: sqlite3
database: db/test.db
test_pgsql:
adapter: postgresql
database: redmine_development
host: localhost
username: user
password: 'password'
When you use rake db:migrate it will try to execute the migrations for all of your environments, this is Production/Development/Test.
If you want to specify the environment use this command instead:
rake db:migrate RAILS_ENV=NameOfTheEnviroment
Related
I am using MAC OS X along with postgresql installed via Homebrew. I am developing using Rails 4.2.1 and ruby 2.2.0. The connection with postgresql server is fine but for some reason every application accesses the database "kstavrou" which is my system username, as a development database and creates the rest as defined by database.yml. That is troublesome if you have more than 1 rails app.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
ecomm_intel_test already exists
strange thing is that if I empty database.yml still connects fine to postgresql and tries to create again the database "kstavrou" executing there all the migrations, without trying to create the test database.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
database.yml
default: &default
adapter: postgresql
host: localhost
encoding: utf8
username: pguser
password: 123456
pool: 5
production:
<<: *default
database: ecomm_intel_prod
development:
<<: *default
database: ecomm_intel_dev
test:
<<: *default
database: ecomm_intel_test
Well the problem was that the ENV['DATABASE_URL'] was set (by some install script) so it was overwriting the database.yml configuration, as noted by steve klein, so I just removed it.
`export DATABASE_URL=postgres:///$(whoami)`
I want to migrate from sqlite3 to postgresql. After doing search I found that I should change my database.yml to somthing like that:
adapter: postgresql
encoding: unicode
database: [insert your dev database name]
pool: 5
username: [insert your user name]
password:
But I don't know what to provide in place of database, username and password. Because i don't remember creating any of them. I just created my rails app and migrations , and thats it.
To configure your rails project with postgres database
do the below steps.
provide your database details in the database.yml file
eg:
development:
username: postgres
database: ur-db-dev
password: pass
encoding: UTF8
adapter: postgresql
timeout: 500
pool: 5
add postgres gem in your Gemfile
gem 'pg'
then,
Do bundle install to install postgres gem
rake db:create to create your database
rake db:migrate to migrate your migration files
I'm just start working with RoR and Heroku hosting.
I want to know how can I run application cloned from Heroku?
I want to add some migration for field. Can I do it right on Heroku without cloning?
What I'm doing:
1) heroku login
2) git clone git.....
3) cd cloned_app_folder
4) bundle install
5) rails g migration add_field_to_posts field:string
On this stage I have many errors: like database.yml not exists and error in configuration.rb, /active_support/lazy_load_hooks.rb and other
6) I'm loaded db schema but it didn't help me
I dont think you can do a straight migration on Heroku.
Check and make sure you have a database.yml in the config folder.
If not and you are using postgres create one that looks something like this
development:
adapter: postgresql
encoding: unicode
database: APP_NAME_development
pool: 5
username: COMPUTER_USER_NAME
password:
test:
adapter: postgresql
encoding: unicode
database: APP_NAME_test
pool: 5
username: COMPUTER_USER_NAME
password:
production:
adapter: postgresql
encoding: unicode
database: APP_NAME_production
pool: 5
username: COMPUTER_USER_NAME
password:
If you already have the above, then run rake db:create and then rake db:migrate.
Then you should be able to run your migration.
Let me know if this doesn't solve the problem!
So after having a problem deploying to Heroku for the first time, I wanted to get my dev environment running Postgres..
That's when i couldn't get my dev environment working. My errors lie in the database. I'm running Rails 3.2, Postgres.app, I got a database setup, I think database.yml is good to go.
As far as I can tell, my schema.rb is blank. It originally was not. When i delete it and reset the database, the new schema.rb is still blank. Where does schema get it's information from?
Rake db:migrate returns this SELECT "schema_migrations"."version" FROM "schema_migrations"
The rails s error complains about the lack of a users table.. doh.
I admittedly edited the schema.rb early on when i was first starting. But I don't know what caused this sort of reset.
database.yml
development:
adapter: postgresql
database: phriends
encoding: utf8
username: mrbubbles
password:
test:
adapter: postgresql
database: test
encoding: utf8
username: mrbubbles
password:
production:
adapter: postgresql
database: production_database
encoding: utf8
username: mrbubbles
password:
Sounds like you didn't rake db:setup, db:setup will
Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
Note the "load the schema" part, that would read schema.rb and build the tables based on what it says. So restore your schema.rb and do a db:setup.
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