Changing database names from SQlite3 to Postgres - ruby-on-rails

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.

Related

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 the connection pool (and other database options) using Heroku

From this page, it looks like Heroku does most of the work in building your database.yml file on deployment, meaning that all I have to have is my development and test environment configurations.
Is there a way I can also specify production values? For example, if I wanted to set pool: 25 on production, can I include that in my database.yml as follows?
development: &default
adapter: postgresql
database: my_app_development
encoding: utf8
host: localhost
min_messages: warning
pool: 2
timeout: 5000
test:
<<: *default
database: my_app_test
-- Can I add this, or will it get overwritten?
production:
<<: *default
pool: 25
Actually, found the answer to exactly my question on the Heroku documentation
https://devcenter.heroku.com/changelog-items/426
Looks like whatever options you set will be merged in and preserved, so I can indeed set connection pool in my database.yml

Can't see my SQLite3 tables in terminal

I opened the SQLite3 console in my terminal and type .tables, but it returns nothing.
This is a problem because I have my website running in the background and I can login, logout, load news, etc.
I don't know where the tables and everything are stored.
This are some of my configuration:
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Previously I was working on Cloud9.io and I could display everything in the terminal.
As I understand it, you launched the sqlite3 command-line utility directly. The thing is, you didn't open a database file with it, which is why it shows no tables.
Instead you should run rails dbconsole or just rails db. That will work even for other database systems, like PostgreSQL (using psql) and MySQL.
However, you could as well use rails console and just fetch your data as you would in Rails.

Is it possible to configure (Thinking) Sphinx on a separate database from the Rails app?

Is it possible to have my app use (in development mode) a SQLite3 adapter, while Sphinx uses Postgres?
I tried to do it by adding the file config/thinking_sphinx.yml below, but I get an error message telling me that SQLite3 is not supported. My config/database.yml has SQLite3 in development, of course.
default: &default
encoding: utf8
host: localhost
adapter: postgresql
port: 5432
pool: 5
timeout: 5000
development:
<<: *default
database: ts_development
test:
<<: *default
database: ts_test
production:
<<: *default
database: ts_production
If you want to use Sphinx, then you need to use either PostgreSQL or MySQL for that environment. So, technically yes, you can use SQLite in development, that will mean you can't use Sphinx or Thinking Sphinx in development, which is I think is a very bad move: I would recommend using the same database in all environments generally (whether or not Sphinx is involved), and especially so in this case, otherwise you won't be able to do anything search-related in your development environment.

Rails Change Default Projects Database 'sqllite' to PostgreSql

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'] %>

Resources