Minimum privileges to run a rails db migration with PostgreSQL - ruby-on-rails

I enrolled into a web-host plan so I could have my rails apps published. They provided me with a server and access to it. This is a shared CentOS Linux server and I can access its bash.
I was trying to load my database schema into the postgres database (v8.4) they are offering but it gives me an error when doing so.
When I execute:
RAILS_ENV=production rake db:schema:load
The following error is generated:
-- enable_extension("plpgsql")
rake aborted!
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "EXTENSION"
LINE 1: CREATE EXTENSION IF NOT EXISTS "plpgsql"
^
: CREATE EXTENSION IF NOT EXISTS "plpgsql"
/home/user/path-to-app/db/schema.rb:17:in `block in <top (required)>'
/home/user/path-to-app/db/schema.rb:14:in `<top (required)>'
PG::SyntaxError: ERROR: syntax error at or near "EXTENSION"
LINE 1: CREATE EXTENSION IF NOT EXISTS "plpgsql"
^
/home/user/path-to-app/db/schema.rb:17:in `block in <top (required)>'
/home/user/path-to-app/db/schema.rb:14:in `<top (required)>'
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)
Because the web-host provider does not allow many db privileges, and due to my unfamiliarity with PostgreSQL, I would prefer a solution changing something on the rails side...
Is there something I can do on the rails side to work around (or hopefully address) this issue?
If nothing can be really done from the rails side I would have to be specific with the privileges necessary.
This PostgreSQL EXTENSION object seems to demand privileges my db user does not have...
What would be the minimum privileges for the rails database user running the db migration in PostgeSQL?
Update
This is about PostgreSQL 8.4, witch according to the comments here do not have the "CREATE EXTENSION" command.

CREATE EXTENSION was introduced with Postgres 9.1, i suggest you to run migrations:
RAILS_ENV=production bundle exec rake db:migrate

Related

Upgrading to Rails 5.2, can't boot, ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation does not exist

With a new database, running bundle exec rails db:structure:load or rails db:create or rails db:migrate or bin/rails db:setup throws
rails aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "clients"
does not exist
LINE 8: WHERE a.attrelid = '"clients"'::regclass
...etc ^
I'd be happy just knowing how to debug this. At some point, some code is calling the Client model, but it's invisible. What could be requiring the clients table to exist pre-boot?
Edits:
No migrations erased, no. The app boots fine locally where there's a database: this first occurs in CI where it's booting from scratch.
Here's the stacktrace https://gist.github.com/EmmaB/01a9322ab66f258a5ebd6883a441251f showing another missing table after I commented out the following code in a Flipper initializer:
Client::Identifiers::IDENTIFER_ID_MAPPING.each do |client_identifier, client_id|
Flipper.register(client_identifier) do |actor|
actor&.client_id == client_id
end
end
But that initialiser code shouldn't run in boot when you're loading the structure, right? And indeed doesn't run in Rails 4.2.10 (upgrading from that to 5.2).
Wrapping model calls in initializers with
Rails.configuration.after_initialize do
# Model calls
end
gets the app to boot. Initializers must be called earlier in the boot process than in Rails 4. Edit: And indeed, here's the thread https://github.com/rails/rails/issues/32870

Errno::EACCES: Permission denied # unlink_internal when running rake test:models

I'm playing with rails 4. I just created very simple test for my model but when I try to execute "rake test:models" I get following error:
C:\rails\project>rake test:models
DL is deprecated, please use Fiddle
rake aborted!
Errno::EACCES: Permission denied # unlink_internal - /tmp/db/new.sqlite3
C:/rails/project/test/test_helper.rb:3:in `<top (required)>'
C:/rails/project/test/models/admin_test.rb:1:in `<top (required)>'
Tasks: TOP => test:models
(See full trace by running task with --trace)
My test database configuration looks as follows:
test:
adapter: sqlite3
database: /tmp/db/new.sqlite3
pool: 5
timeout: 5000
"/tmp/db" is directory where I have full access on my personal laptop.
Any ideas what could be source of this problem?
One way I was able to get around this error was using
rake db:migrate rails_env=test
This generated the necessary migrations on my test.sqlite3
I believe the default setting for rails_env is "dev" but this may vary. I am still very new to rails but I hope this helps.
I had the same problem with my test.sqlite3 table and what worked for me was making a copy of my development.sqlite3 table and renaming it test.sqlite3. I hope this works for you.

PG::ConnectionBad Error when trying to run rake db:migrate after creating model, using postgresql

Trying to get my rails up and running but am having a problem. When creating my new rails app on the command line I ran the usual
rails new PhotoApp -d postgresql
Generated my core scaffold. Then generated a model with attributes in the terminal and that was fine. After creating my model I first ran rake db:create and that returned a long log of characters with at the top telling me FATAL: role "PhotoApp" does not exist so then I tried rake db:migrate and that didn't work either returning me
rake aborted!
PG::ConnectionBad: FATAL: role "PhotoApp" does not exist
/Users/##$%^$#/Code/Projects/PhotoApp/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
WTF is going on here?
Thanks for any help.
The error is telling you that Postgres isn't finding your app. What does your database.yml file look like?
I would first run the rake task again, using the --trace as suggested:
rake db:create --trace
That will give you a more verbose tracing, so you can try to isolate the problem.
From experience I can tell you though that 'Roles' in Postgres can be a nightmare. How did you install Postgres on your machine? HomeBrew? The Postgres App?
Please post the full trace, as well as your database.yml file. This will help better assess the problem!

ruby on rails rake db:migrate aborted

I am new on ruby on rails, I try to look for the answer to this type of problem. i have been looking for a long time and i haven't been able to find a solution.
I am working on windows vista home, ruby 2.0.0, rails 4.0.2.
I have try different ways to solve the problem and it seems that I do not understand what the output is telling me:
$ rake db:migrate
rake aborted!
LoadError: cannot load such file -- sqlite3/database
c:/Users/me/desktop/family_N/config/application.rb:7:in `<top (required)>'
c:/Users/me/desktop/family_N/Rakefile:4:in `<top (required)>'
Thank you for your fast reply.
I install devise and try rake db:create, and i got the same output.
Forgive my ignorance but, this is what I am looking at
db/migrate: 201405282050240_devise_create_user.rb.
db/schema.rb: ActiveRecord::schema.define(version: 20140617211300) do |t| ...
do this dates have to match?
Did you create your database? From the error posted, it would appear that you had not yet done so.
rake db:create
You do need to create the DB before you can migrate data to it.
try bundle exec rake db:create

Migrate not working on Heroku

I've run pg:reset on Heroku and on trying to run db:migrate, all migrations run but the migration fails with the following error and trace:
rake aborted!
Error dumping database
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/tasks/postgresql_database_tasks.rb:55:in `structure_dump'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/tasks/database_tasks.rb:142:in `structure_dump'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:288:in `block (3 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:51:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:45:in `block (2 levels) in <top (required)>'
As can be seen here the problematic line and the one above it are:
command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}"
raise 'Error dumping database' unless Kernel.system(command)
This works locally without any problems, both in development and production environments.
Has anyone experienced anything like this?
This is an interesting bug that, as it turns out, can be ignored. Based on the fact it's attempting to do a db:structure:dump, you are using 'sql' as your active_record.schema_format. The rake task db:structure:dump will fail on heroku because pg_dump is (unsurprisingly) not in the binary path. Interestingly enough, Heroku states that db:schema:dump is not supported but if you set the schema format to ruby, it works fine.
In Rails 3, the dump task would only raise an error is the exit code of the command was 1. On unix based systems if the command is not found, the exit code is 127. So even if the pg_dump command fails on rails 3 (which it does), it won't raise an error and it won't halt the rake task. So anyone using a sql schema format with Rails 3 wouldn't have this issue because it would fail silently. The refactor in Rails 4 to properly raise an error if the dump failed causes db:migrate to raise an error on Heroku. However, even though it errors with rake aborted the ddl is actually performed and committed.
Possible solutions:
Ignore the error as the migrations are actually running.
Since you don't care about the structure dump on production, set the schema_format to ruby. In config/environments/production.rb:
config.active_record.schema_format = :ruby
If for some reason you don't want to change the config file: add a rake task with the following to suppress the error:
if Rails.env == 'production'
Rake::Task["db:structure:dump"].clear
end
The accepted solution is somewhat correct. Heroku does have pg_dump on its runtimes
$ heroku run bash
$ which pg_dump
/usr/bin/pg_dump
The problem comes from this issue: https://github.com/rails/rails/issues/21226 the command cannot be run correctly.
If you need to do a db:structure load instead you can use the $ heroku pg:psql
heroku pg:psql -a your-app-name <db/structure.sql
From heroku rake db:structure:load failure.
If you need to dump you can use this article https://devcenter.heroku.com/articles/heroku-postgres-import-export there's also dedicated commands:
$ heroku pg:pull <REMOTE_SOURCE_DATABASE> <TARGET_DATABASE> # pull from REMOTE_SOURCE_DATABASE to TARGET_DATABASE
$ heroku pg:push <SOURCE_DATABASE> <REMOTE_TARGET_DATABASE> # push from SOURCE_DATABASE to REMOTE_TARGET_DATABASE
If you need to reset your database before running migrations you can use $ heroku pg:reset

Resources