Rails Change Default Projects Database 'sqllite' to PostgreSql - ruby-on-rails

I'm learning ruby. I've created some application and played with it.
i wanted to change the database to postgres. I modified gemfile with
gem 'pg'
[pg already installed in usr/bin/pg]
then update database.yml with
postgresql where sqllite is used.
pg connection bad:
FATAL: role "user" does not exist
i've created some postgres user already. what should i have to change to work with postgres?
development:
adapter: postgresql
database: db/development.postgresql
pool: 5
timeout: 5000
user:
adapter: postgresql
database: db/test.postgresql
pool: 5
timeout: 5000
production:
adapter: postgresql
database: db/production.postgresql
pool: 5
timeout: 5000

If you don't specify a username to connect to PostgreSQL as, libpq will connect using the login username of the current running user.
libpq is what the Pg gem used by Rails uses to connect to PostgreSQL.
So, in your database.yml, specify the PostgreSQL user to connect to the database as with a user: entry. Make sure this corresponds to a user that exists in PostgreSQL, and that pg_hba.conf permits this user to connect.
To learn more, see the client authentication chapter of the PostgreSQL docs.

I know its a two year old question, but I just ran into the same problem, and i fixed it by changing the syntax of config.yml, then I rake db:migrate
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: yourappnamehere_development
test:
<<: *default
database: yourappnamehere_testdevelopment:
production:
<<: *default
database: YOURAPPNAME_production
username: YOURUSERNAME
password: <%= ENV['YOURAPPNAME_DATABASE_PASSWORD'] %>

Related

Changing database names from SQlite3 to Postgres

I am currently trying to switch from SQlite3 to Postgres, and was wondering how I can change my database name? According to Heroku, they say this:
Note the adapter name is postgresql not postgres or pg. You will also
need to change the database: to a custom name. A final version might
look something like this:
development:
adapter: postgresql
database: my_database_development
pool: 5
timeout: 5000
test:
adapter: postgresql
database: my_database_test
pool: 5
timeout: 5000
production:
adapter: postgresql
database: my_database_production
pool: 5
timeout: 5000
I see right now that I have the following for my database name for test environment:
test:
<<: *default
database: db/test.sqlite3
For this (and dev and production environments) do I need to simply change the name of the database, or do I need to do something more than this. Because for the SQLite3 files, it has a .sqlite3 extension. Does postgres have a similar situation?
Yes, you can just change the database name for each additional environment.
The
<<: *default
means that is the part of the configuration that will be used in the other environments, so you don't have to duplicate your code (DRY).
For postgres you don't have to give extensions (as I remember).
Just give the databases clear names, and that should work.

Can I easily and safely reset Rails database to Postgres from SQlite after building the app?

I'm a beginner in Rails and I'm curious whether I'm better off setting the database to Postgres from the beginning or I can just use SQLite and forget about it until later. And if setting it in the beginning is better, should I simply follow the 3.12 in http://guides.rubyonrails.org/configuring.html#configuring-a-database or are there more things to be done? It seems like I need to do more but it's not really clear. For example, other than adapter, which the instruction says to change, there are more code with SQLite built-in and I'm not sure whether I need to do anything to them as well:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
If you intend to use Postgres than it makes sense to setup the application to use it from the get go.
While it is possible migrate data from SQLite to Postgres and vice-versa you would be better off not having to deal with the potential issues that can occur.
In general you want to develop and test on the same database system you are deploying to as they have different features and subtile differences in how they interpret the SQL standards. You don't want to find them out by pushing to the production server and then getting a very angry phone call.
This can definitely can bite you in the *rse when deploying to Postgres as it is rather strict (its a good thing).
A decent config would be:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: my_app_dev
test:
<<: *default
database: my_app_test
production:
<<: *default
database: my_app_production
Note that on Heroku and other Platform-as-a-Service (PaaS) services you can simply leave out the production section as it will be overwritten in a post commit hook.
See also:
http://12factor.net/dev-prod-parity
You can install the Postgresql gem using either:
gem install pg in your terminal
or
gem 'pg' in your Gemfile.
Then you just need to go into config/database.yml and set up something similar to this:
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
From there, just run your migrations, and you should be all set up!

setting up postgresql development in rails with multiple users in database.yml?

I am in the process of migrating my SQL to Postgresql because I want to develop in the same environment as my production.
I installed Postgres and was able to set my database yml file such that rake db:create:all worked with no problems.
However before I pushed, I realized that the username and password of the postgresql database is only available on my computer. I.e. I created the login role on my computer. When my friend who is also developing on the application, pulls the code, he won't have the username and password created by me on my computer.
Is he supposed to also create the same login role? Or is there a way to leave username / password blank so that everyone who develops can access the application? More importantly, what is the best practice for a situation like this?
I am developing on windows and he is on mac.
My database.yml: username and password left out.
development:
adapter: postgresql
encoding: unicode
database: volatility_development
pool: 5
timeout: 5000
host: localhost
test:
adapter: postgresql
encoding: unicode
database: volatility_test
pool: 5
timeout: 5000
host: localhost
production:
adapter: postgresql
encoding: unicode
database: volatility_production
pool: 5
timeout: 5000
one possibility would be to use a combination of erb and environment variables, for example:
development:
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
adapter: postgresql
encoding: unicode
database: volatility_development
pool: 5
timeout: 5000
host: localhost
then you could do either of the following:
start your server like so: USERNAME=bob PASSWORD=secret rails s
add the following to your .bashrc
export USERNAME=bob
export PASSWORD=secret

Rails: How do I change my database from SQLite to PG while in Development?

I generated my rails app with the default SQLite database, but after creating a few models and migrating a few times, I want to change it to Postgresql.
I added the postgres gem to my Gemfile, bundle install, then I replaced all my database.yml code from
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
to
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
password: mypass
development:
<<: *default
database: sample_app_development
test:
<<: *default
database: sample_app_test
production:
<<: *default
database: sample_app_production
I get a FATAL: password authentication failed for user "postgres" error even though the password is correct. Is it because I am missing a step? Am I supposed to tell PG using pg Admin III that I want to add this app to my server? Am I supposed to create a new role/connection?
I have run into this problem a few times and don't seem to be able to find an answer for this specific problem.
it gives me this when I try to run rake db:drop :
Couldn't drop sample_app_development : #<PGError: FATAL: role "postgres" does not exist
>
Couldn't drop sample_app_test : #<PGError: FATAL: role "postgres" does not exist
>
=========
Edmunds-MacBook-Pro:sample_app edmundmai$ createuser foo
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
Password:
createuser: could not connect to database postgres: FATAL: password authentication failed for user "edmundmai"
Postgres user authentication is a bit weird. The default is to use the same authentication as the OS (at least in Linux). So to get to the Postgres prompt from the command line, you have to do something like this:
sudo -u postgres psql
Note that there's no password - and because the OS takes care of the authentication, there's no need for one (the OS'll ask for your sudo password, though, if required).
So option one is to just strip the password option out of your Rails config file and hope everything works out. Failing that, set up Postgres to accept password-based authentication by editing the pg_hba.conf file (mine's at /etc/postgresql/9.2/main/pg_hba.conf). Here's an example from my local server; the user "postgres" uses the OS's authentication ("peer"), but the user "opengeo" uses a password ("md5"):
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all opengeo md5
Hope that helps!
To convert your database to postgresql first create a user as below:
$ createuser foo
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
To create a db:
CREATE DATABASE foo_db ENCODING 'UTF8' OWNER foo;
make sure your database.yml looks as below:
development:
adapter: postgresql
encoding: unicode
database: foo_db
pool: 5
username: foo
password:
test:
adapter: postgresql
encoding: unicode
database: foo_test
pool: 5
username: foo
password:
development:
adapter: postgresql
database: postgres
username: postgres
password: ;ernakulam
pool: 5
timeout: 5000
test:
adapter: postgresql
database: postgres
pool: 5
timeout: 5000
production:
adapter: postgresql
database: postgres
pool: 5
timeout: 5000`

Change from SQLite to PostgreSQL in a fresh Rails project

I have a rails app that's databases are in SQLite (The dev and production). Since I am moving to heroku, I want to convert my database to PostgreSQL.
Anyways, I heard that the local, development, database does not need to be changed from SQLite, so I don't need to change that, however, how do I go about changing the production environment from SQLite to PostgreSQL?
Has anyone ever done this before and can help?
P.S. I'm not sure what exactly this process is called, but I've heard about migrating the database from SQLite to PostgreSQL, is that what needs to be done?
You can change your database.yml to this instead of using the out of the box sqlite one:
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test: &TEST
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
cucumber:
<<: *TEST
The steps below worked for me. It uses the taps gem, created by Heroku and mentioned in Ryan Bates's Railscast #342. There are a few steps but it worked perfectly (even dates were correctly migrated), and it was far easier than the Oracle -> DB2 or SQL Server -> Oracle migrations I have done in the past.
Note that SQLite does not have a user id or password, but the taps gem requires something. I just used the literals "user" and "password".
Create the Postgres database user for the new databases
$ createuser f3
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
EDIT - Updated command below - use this instead
$ createuser f3 -d -s
Create the required databases
$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test
Update the Gemfile
gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle
Update database.yml
#development:
# adapter: sqlite3
# database: db/development.sqlite3
# pool: 5
# timeout: 5000
development:
adapter: postgresql
encoding: unicode
database: f3_development
pool: 5
username: f3
password:
#test:
# adapter: sqlite3
# database: db/test.sqlite3
# pool: 5
# timeout: 5000
test:
adapter: postgresql
encoding: unicode
database: f3_test
pool: 5
username: f3
password:
Start the taps server on the sqlite database
$ taps server sqlite://db/development.sqlite3 user password
Migrate the data
$ taps pull postgres://f3#localhost/f3_development http://user:password#localhost:5000
Restart the Rails webserver
$ rails s
Cleanup the Gemfile
#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle
Now its become easy with the single command
bin/rails db:system:change --to=postgresql
Since you're moving to heroku, you can use taps to do this:
heroku db:push
This will push your local development sqlite data to production, and heroku will automagically convert to postgres for you.
This should also work to push a production sqlite db to heroku, but it's not tested.
RAILS_ENV=production heroku db:push
you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.
Simply update the config/database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: projectname_development
test:
<<: *default
database: projectname_test
production:
<<: *default
database: projectname_production
username:
password:
The above is what's generated when you run:
$ rails new projectname --database=postgresql --skip-test-unit
Also add this to your Gemfile:
gem 'pg'
Just Update you datatbase.yml
development: &development
adapter: postgresql
database: Your_database_name
username: user_name
password: password
host: localhost
schema_search_path: public
min_messages: warning
test:
<<: *development
database: test_database_name
production:
<<: *development
database: production_db_name
We are using rails and the basic standards should be follow like DRY, Convention over Configuration etc.. so in above code we are not repeating same code again and again.
It's been mentioned above me, but I don't have enough reputation as a lurker to be able to upvote it. In the hopes of drawing a little more attention for Rails newbies reading this answer:
you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.
^^^ This is a key piece in addition to the database.yml file described in the selected answer to migrate your Rails app to Postgres.
After replacing gem 'sqlite3 with gem pg in the gemfile, I kept getting the sqlite3 error when pushing to Heroku master because I forgot to commit the updated gemfile. Simply doing the following solved this:
git add .
git commit -m 'heroku push'
heroku create
git push heroku master
This is how I have mine setup. If you are only using MRI and not Jruby you can skip the logic in the adapter settings.
defaults: &defaults
adapter: <%= RUBY_ENGINE == 'ruby' ? 'postgresql' : 'jdbcpostgresql' %>
encoding: unicode
pool: 5
timeout: 5000
development:
database: project_development
<<: *defaults
test:
database: project_test
<<: *defaults
production:
database: project_production
<<: *defaults
You can try following:
sqlite3 development.db .dump | psql dbname username
or try with sqlitetopgscript:
http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg
A possible solution (not for heroku) it's to use yaml.db from:
http://www.railslodge.com/plugins/830-yaml-db
Today I had the same issue. I'm working on Rails 4.2.8. The solution was specify the pg gem version, in my case, 0.18.4.

Resources