trying to understand postgreSQL database after migrating from SQLite3 - ruby-on-rails

I used to develop on SQLite3 and deploy on PostgreSQL. However, after this question I realised the importance of having the same database for development and production.
After many problems, I finally managed to install the PostgreSQL on my Mac OS X Lion 10.7.4. However, I am still facing some problems:
Using SQLite3 the databases' path is defined on the database.yml, which rails automatically creates within each new project.
Using PostgreSQL the database name is defined on the database.yml, which rails doesn't create automatically.
My question:
Shall I create a new database for each new project or shall I change the database.yml to include the same database for all projects?
I saw people saying that each user in a computer should have one database, when using PostgreSQL.
Well, if the best practice is to create one database for each project (which I hope so)...
There is any way to configure rails to create the new database automatically within each new project?

Creating a DB is mostly one-time operation, so it's not a big deal to create it once for a project. A casual Postgresql admin can use pgAdmin graphical tool.

If you use Postgresql for development, you need to create database by your self.
But you can initialize you project for Postgresql.
rails new my_project -d postgresql
Have a good day.
Benjamin.

Problem solved:
I have followed this tutorial and I could finally solve the problem.
I have created a new user for my postgreSQL called user1 without any password, so each time I create a new project I just need to change the database.yml for the below:
development:
adapter: postgresql
encoding: unicode
database: newproject_development
pool: 5
username: user1 #I am changing this line
password:
test:
adapter: postgresql
encoding: unicode
database: newproject_test
pool: 5
username: user1 #I am changing this line
password:
production:
adapter: postgresql
encoding: unicode
database: newproject_production
pool: 5
username: user1 #I am changing this line
password:
As the the user1 doen't have any password, now I can just create the tables using the Rails rake.
rake db:create:all

Related

Ruby on Rails and SQLite encrypted database

I am using Rails 5.2.2 and SQLCipher Version 3.2.0 (based on SQLite 3.8.6).
I have encrypted the SQLite database using sqlcipher but I am not able to load the rails application. The message is get is as follows:
SQLite3::NotADatabaseException: file is encrypted or is not a database: SELECT name FROM sqlite_master WHERE name <> 'sqlite_sequence' AND name = 'schema_migrations' AND type IN ('table')
I think that my issue lies in updating the file config/database.yml and specifying the password. I have tried specifying options like password, key, etc. but with no success.
I was wondering whether it is at all possible to use encrypted SQLite database in rails?
It's not a matter of which version of Ruby you are using, it's a matter of does there exist an adapter for the database you would like to use.
Rails tries to talk to all databases the same way, each database technology needs to provide an adapter to translate what Rails is asking for into the commands needed by that database technology to accomplish what Rails is asking for.
This configuration is done in the config/database.yml file, which might look something like the following (notice the adapter stanza):
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
There are many database adapters available, including SQLite, but I have not seen any for SQLCipher, you might have better luck searching than I did.
From what I have seen most Rails apps don't encrypt the entire database, but do however encrypt certain columns, for example storing some encrypted version of a password or access token.

rake db:migrate is creating tables from other rails projects

I have created a new rails project and I have added a migration to create a table called blogs. When I ran rake db:migrate it created this table along with other tables I have set in different projects.
Under db/migrate I have only 1 file with the migration I added, there's no reference in the project of the other tables, for some reason it is pulling the migrations from other projects and adding it to my DB project
Rails version:
5.1.4
Ruby version:
2.4.1
Probably you're using the same database. You need to specify different database in the config\database.yml file for each project. You can have same username, same password, etc. but At least the database: (database name) have to be different if you don't want all your tables in one database
You need to configure your database.yml for each project.
And create 3 databases for development, test and production.
And specify all the information needed to access your database.
Eg:
development:
adapter: postgresql
username: postgres
password: blogs_postgres
database: blogs_development
encoding: utf8
host: localhost
pool: 5
timeout: 5000
Refer Here for more info.
I just solved my issue, seems like Spring was mixing things up in my environment.
Reference: https://github.com/rails/rails/issues/31529#issuecomment-353269787

When trying to generate a model with Rails and PostgreSQL, the command hangs without error

When using this command:
rails generate model Event name:string
Nothing happens, and I have to CTRL+c.
The versions I'm using are:
Ruby 2.1.1p76
Rails 4.1.0
PostgreSQL 9.3.4
Mac OS X 10.9.3
I've verified that Postgres is working and I can connect to it by running psql easyEventTracker_development. I setup my rails project by running rails new easyEventTracker -d postgresql
When running rake db:create, I get the following message:
easyEventTracker_development already exists
easyEventTracker_test already exists
Contents of the database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: easyEventTracker_development
test:
<<: *default
database: easyEventTracker_test
production:
<<: *default
database: easyEventTracker_production
username: easyEventTracker
password: <%= ENV['EASYEVENTTRACKER_DATABASE_PASSWORD'] %>
If I setup the project using Rails with SQLite, everything works fine.
There is a bug in Spring with Rails 4.1 which causes generators to hang.
Try running spring stop and running the generator again.
It appears you are not providing username and password for your development and test databases. You should verify you are able to connect to your database using
rails dbconsole
which will provide a database specific SQL-prompt that uses the credentials you provide in database.yml. While sqlite3 is a file based program which does not need login credentials you will need them even for development mode if you use a database that is able to use different schema.
If you set up your development schema to work without a password you will still need to provide the schema name as username as in
username: easyEventTracker_development
Same thing happened to me and I wasn't using Spring so the solution didn't work.
I just quit the console, opened it up again, ran the same command and it worked.

Ruby on Rails server stopped working

I am new in ror developement..i was working on a LIVE server...I just uploaded a file through sftp...after 1 day server suddenly stopped working...You can see the error message from here
it shows
There appears to be a database problem.
Your config/database.yml may not be written correctly. Please check it and fix any errors.
Your database schema may be out of date or nonexistant. Please run rake db:migrate to ensure that the database schema is up-to-date.
The database server may not be running. Please check whether it's running, and start it if it isn't.
Looking at the error page you seem to be using Rails 2.3?
At a guess you have a MySQL database not an SQLite running. You should have the user name and password for the database around somewhere (replace the relevant fields in the 3 sections with them).
Change the database names to reflect your database names.
The server admins might have set a specific socket for MySQL in which case replace the '/tmp/mysql.sock' with the socket number.
Check your Gems to see if the MySQL adapter is installed (you appear to be using Rails 2.3 so try gem list on the terminal for your server - make sure that you are in the root directory for the app).
If the MySQL gem is missing use gem install to install it (this will depend on what your hosting provider allows).
The following links are pretty old - targetted towards Rails 2 which you appear to be using.
http://www.ruby-forum.com/topic/139710
http://forums.mysql.com/read.php?116,353922,359544
database.yml
development:
adapter: mysql
encoding: utf8
database: temp_development
username: root
password:
socket: /tmp/mysql.sock
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
encoding: utf8
database: temp_test
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
database: temp_production
username: root
password:
socket: /tmp/mysql.sock

rails 3 postgreSQL basic 'database does not exist'

OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword

Resources