rake aborted! ERROR: must be owner of database - ruby-on-rails-3

I am working through Michael Hartl's excellent tutorial but when trying to prepare the test database with the command:
bundle exec rake db:test:prepare
I get this error message:
ERROR: must be owner of database sample_app_test...
which I never got when using the development database, because I had created the following database role for my Rails app:
CREATE ROLE demo_app WITH CREATEDB LOGIN
(this is using Postgresql)
Does anyone understand why this is failing in the test environment?
TIA...

Did you ensure the ownership of the test DB? try running the \l command on Postgres console client and check the ownerships. you can also try the following query:
ALTER DATABASE sample_app_test OWNER TO demo_app;

First post, writing this down for posterity. I was having the same problem but was able to fix it. You just have to make sure you were/are signed in as a superuser when you create your databases (or the one that is throwing the error).
I was logging into psql with this code:
sudo sudo -u postgres psql
And created my databases. This is bad. You want to log in with these superuser credentials:
sudo su - postgres
And then after you're logged in to postgres:
psql
Then create your databases. You can kill your old databases with the command
DROP DATABASE "database_to_drop";
Recreate them and you should be good to go!

Related

Having issues with command line and Postgres 12 psql

I am a complete novice. I am following the freecodeacademy tutorial on Postgres and set up the environment fine with the bin and lib.
My issue comes with his next instructions which insinuate I can just put 'psql' and then it will open my environment to postgres. This is not the case for me. It actually prompts me to enter my password for my User profile as in my laptop userprofile and when I do, the password is marked as incorrect. for example, it's not showing C:\Program Files\PostgreSQL\12\bin> like I pathed it in the environment settings. Just my regular user profile.
The only way I have been able to enter the environment is using 'psql -U postgres' and then entering the password I set for it.
psql --help also doesn't work.
I was hoping anyone had any advice for me as a newbie (alternative resource/solution-wise)? I don't have cmd line or any coding experience so my troubleshooting with this is not good because it's very foreign to me.
Thanks for any help
You probably want to connect to the postgres database as user postgres:
psql -U postgres -d postgres
You can set the environment variables PGUSER and PGDATABASE to the user and database you want by default.

PostgreSQL dump from Ruby on Rails controller

I have implemented ruby on rails app and I want to have a very easy way to create save points and load them from the view of this app.
For now, before I implement a huge undo-stack, I want to do a SQL dump into a file by a ruby on rails controller method and also load the dumped file back into the database. How can I perform this?
First, I wonder what the problem you are actually trying to solve is - maybe something like database transactions would make sense here?
Assuming they don't, however, and you do need to get a full snapshot of the database and restore it, it is going to depend on what database you are using. I'm most familiar with Postgres and I know there exists pg_dump and pg_restore commands to do this type of thing.
https://coderwall.com/p/2e088w/rails-rake-tasks-to-dump-restore-postgresql-databases has a walkthrough of the actual commands needed, and does it in the form of a Rake task. If you are wanting to call it from the controller, however, I would pull those out into a new class that the controller can tell to dump or restore as needed.
Finally I work out an answer, how to dump and restore if you are using a PG Database in Rails.
The problem with pg_restore is that the command cannot drop the database while other users (eg. rails server) are accessing it:
To dump the database:
cmd = "pg_dump -F c -v -U YOUR_ROLE -h localhost YOUR_DATABASE_NAME -f db/backups/hello.psql"
To restore:
system "rake environment db:drop"
system "rake db:create"
system "pg_restore -C -F c -v -U YOUR_ROLE -d YOUR_DATABASE_NAME db/backups/hello.psql"
Finally to get the rake environment db:drop to work you have to use this monkeypatch taken from
# config/initializers/postgresql_database_tasks.rb
module ActiveRecord
module Tasks
class PostgreSQLDatabaseTasks
def drop
establish_master_connection
connection.select_all "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='#{configuration['database']}' AND state='idle';"
connection.drop_database configuration['database']
end
end
end
end

Is there a better way than this to run an SQL script through puppet?

Take a look at Get puppet build to fail when the contained SQL script fails execution
I was attempting to run a vagrant build which installs Oracle XE in an Ubuntu Virtualbox VM and then runs a an SQL script to initialize the Oracle Schema. The vagrant build is here : https://github.com/ajorpheus/vagrant-ubuntu-oracle-xe. The setup.sql is run as a part of the oracle module's init.pp (right at the bottom or search for 'oracle-script').
When running the SQL script as a part of the vagrant build, I see the following error:
notice: /Stage[main]/Oracle::Xe/Exec[oracle-script]/returns: Error 6 initializing SQL*Plus
notice: /Stage[main]/Oracle::Xe/Exec[oracle-script]/returns: SP2-0667: Message file sp1<lang>.msb not found
notice: /Stage[main]/Oracle::Xe/Exec[oracle-script]/returns: SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
There were two things that were instrumental in me finding a workaround for the problem:
As suggested in this answer, setting the logoutput attribute to true for the exec block under question immediately showed me the error, whereas before the exec was just failing silently.
It seemed strange that I was able to run the command (sqlplus system/manager#xe < /tmp/setup.sql) after manually logging in as the 'vagrant' user. That suggested that there was something missing in the environment. Therefore, I copied all ORACLE env. vars into the exec as seen on Line 211 here
That worked, however, setting up the env vars manually seems a bit brittle. Is there a better way to setup the ORACLE environment for the vagrant user? Or, is there a way to get puppet to setup the environment for the vagrant user similar to an interactive shell?
If some profile has been set up to give the user a working interactive shell, you should be able to pass your action through such a shell
command => 'bash -i -c "<actual command>"'
As an aside about logoutput, since you mentioned that - the documentation advises that "on_failure" is a sane default, as it will only bloat your output when there are actual errors to analyze. It is the actual default in the latests versions of Puppet.

working with sqlite3 in mac os x 10.9

I know that Mac OSx has pre-installed sqlite3.But when I am entering this command, when in
/usr/bin directory, sqlite3 test.db i get this displayed on terminal..
Adityas-MacBook-Air:bin adityabahuguna$ sqlite3 test.db
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
Its fine till now. But when i try to create a table by
sqlite> create table t(name text);
I always get this error:
Error: unable to open database "test.db": unable to open database file
This however does not occur when i directly use create table command without creating a new database. I want to figure out the way to create a new database (say test.db as above).
You're trying to create the database in /usr/bin where normal users don't have write access.
You can write to /usr/bin with root/sudo as suggested by Atul but that's not really something you should do.
Instead, specify a database path that is writable by your normal user, for example
sqlite3 ~/test.db
so the test.db gets created in your home directory.
you need to be logged in as root user to have create database privilege. you can become root user by following these steps: How to Become root user.

Uable to run migration on heroku on a postgre database

I need to add a property/column to a table in a production database(Postgre) on Heroku.com (Rails app) by doing migration.
When I do the migration it looks ok, but when I view the columns on the table it has has not added the column!
My development db is sqlite3 and the production db is postgre
I do the following:
heroku run rails generate migration AddUtc_OffsetToEvents utc_offset:integer RAILS_ENV=production --app app-name-1111
and it returns:
invoke active_record
create db/migrate/20130304070946_add_utc_offset_to_events.rb
And then I run the migration
heroku run rake db:migrate RAILS_ENV=production --app app-name-1111
And then:
heroku restart
When I run
heroku pg:psql HEROKU_POSTGRESQL_GRAY_URL --app app-name-1111
and check the columns of the table:
\d+ events
It still does not have the utc_offset column, and no errors are displyed while doing the previous cmds.
Any ideas or hints?
It looks like you are doing several calls to heroku run
Each time you do heroku run it spins up a completely new dyno with your latest code, and when run is over that dyno is destroyed. So the second heroku run does not have the migration filed created in the first.
Since you are already familiar with psql you can just use ALTER TABLE directly. Otherwise you'll need to check your migration into your code and git push heroku master it to heroku, then run it.
Why not just download the code, add the migration and push the changes ?
After, just run the heroku run rake db:migrate on the app.