I'm using the apartment gem for my SaaS (built with Ruby on Rails) and one of its requirements is we need to install the hstore PostgreSQL extension via a dedicated schema (ie: shared_extensions), not the default public schema.
Heroku enables hstore by default; however it is installed in the public schema. When I tried to do:
ActiveRecord::Base.connection.execute("ALTER EXTENSION hstore SET SCHEMA hstore;")
the console returned:
(3.6ms) ALTER EXTENSION hstore SET SCHEMA hstore;
PG::Error: ERROR: must be owner of extension hstore
Currently it's impossible on the code side to make do with hstore extension being installed on public, so I definitely have to find a way to put hstore extension on shared_extensions schema.
Will there be a way to do this on Heroku? Thanks.
A less destructive alternative work around:
1.) Put your application in maintenance mode:
heroku maintenance:on -a [app-name]
2.) Backup up your database: ALWAYS BACK IT UP :)
heroku pg:backups capture -a [app-name]
3.) Copy your database locally:
heroku pg:pull [Database URL] mylocaldb -a [app-name]
4.) Now you have the db locally, you can move the extensions without any destruction of data or columns because you have local superuser priv.:
ALTER EXTENSION "hstore" SET SCHEMA hstore;
5.) Now export this version back to production:
heroku pg:push mylocaldb [Database URL] -a [app-name]
6.) Take your app out of maint. mode and you now have the extensions installed in the shared schema of hstore.
7.) You will still need to perform step 1 of Tu H.'s response if you are on rails < 4.1
OR if you are running rails 4.1+ you can just update your database.yml file:
schema_search_path: "public,hstore"
This is a less destructive way to get what you want done without too much downtime.
Turns out I can't do ALTER EXTENSION hstore SET SCHEMA hstore; on Heroku. I've documented the workaround here
Append ?schema_search_path=public,hstore to your DATABASE_URL environment variable, by this you don't have to revise the database.yml file (which is impossible since Heroku regenerates a completely different and immutable database.yml of its own on each deploy)
Run heroku pg:psql from your command line
And then DROP EXTENSION hstore; (Note: This will drop all columns that use hstore type, so proceed with caution; only do this with a fresh PostgreSQL instance)
Next: CREATE SCHEMA IF NOT EXISTS hstore;
Finally: CREATE EXTENSION IF NOT EXISTS hstore SCHEMA hstore; and hit enter (\q to exit)
Related
i have multiple databases in my project and i want to open rails console to access both databases.
currently i can fetch data of only one default database.
In other word, how to open specific database console?
In other word, how to open specific database console?
From official guide:
bin/rails dbconsole figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL (including MariaDB), PostgreSQL, and SQLite3.
You can also use the alias "db" to invoke the dbconsole: bin/rails db.
If you are using multiple databases, bin/rails dbconsole will
connect to the primary database by default. You can specify which
database to connect to using --database or --db:
bin/rails dbconsole --database=animals
So command is
rails db --db=db_name_from_database_yml
For example you have in your database.yml
production:
my_primary:
adapter: postgresql
database: some_db_name
In this case your command will be
bundle exec rails db --db=my_primary -e=production
Why don't you have a read through the documentation https://guides.rubyonrails.org/active_record_multiple_databases.html.
You should be able to find just what you need. connects_to does the trick of database switching automatically of manually.
Or
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html#method-i-connected_to
ActiveRecord::Base.connected_to(database: :mydb) do
# Do something here...
end
How can I insert new records to Heroku database. I have successfully uploaded my application and database structure. Now I need to insert some records so it could work. I tried to use seeds.rb but without any success. db:push and db:seed don't give any success. The database is always empty. Is there any heroku tool available which I could use to see database structure and insert data?
Thank you.
You have a few options. You can run heroku run rake db:seed.
Or assuming Postgres, try
heroku pg:psql
And run your SQL commands from there. See here for more.
I was also able to connect my IDE to my databases on Heroku. I am using RubyMine, so it was a matter of using the right JDBC URL to make that happen.
You can use below command for insert record to Heroku server
heroku run rails c
Example:-
suppose you have one table is employee and field are first_name.
Now open a command line and call this command
heroku run rails c
After call insert command
Employee.create(first_name: "admin")
I've been given access to Heroku application with rather strange setup. It has one database but when I run heroku config, I get different DATABASE_URL and HEROKU_POSTGRESQL_BRONZE_URL.
When I run heroku pg:info I get the following result:
=== HEROKU_POSTGRESQL_BRONZE_URL
Plan: Dev
Status: available
Connections: 1
PG Version: 9.2.4
Created: 2013-09-05 11:02 UTC
Data Size: 6.5 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
I realised that my database is at DATABASE_URL, but I can't access that database, only through heroku run console. All heroku pg commands fail with this message:
! Unknown database: DATABASE_URL. Valid options are: HEROKU_POSTGRESQL_BRONZE_URL
If I run heroku pg HEROKU_POSTGRESQL_BRONZE_URL, I get access to empty database from above.
Since I have some issues with running migrations, I think my database might be full, and I'd like to check it. Any ideas how I can do that?
Here's the error after I run heroku run rake db:migrate:
PG::Error: ERROR: permission denied for relation schema_migrations
: INSERT INTO "schema_migrations" ("version") VALUES ('20130918114202')
More information about the setup:
rails 3.2.12
RAILS_ENV: staging (I don't have access to production, but I know this is "dev" server and there's also real "staging" from which this app was forked).
I have same problem and i fixed it:
Just Keep a Backup from your database and restore it back again, here is the steps:
heroku pg:info <-- to get the Database Name
heroku addons:add pgbackups <-- make sure you have the addons for backup
heroku pgbackups:capture <-- Capture the backup
heroku pgbackups <-- check your backups and make sure its there
heroku pg:reset DATABASE_NAME <-- Reset your database don't worry we have a backup, replace DATABASE_NAME with database name
heroku pgbackups:restore DATABASE_NAME b001 <-- Restore the backup again, replace DATABASE_NAME with database name and b001 with your Database version you can see this version number in heroku pgbackups step
heroku run rake db:migrate <-- Now you can run your migration and Operate in normal mode.
This seems like something screwy on the Heroku side. Have you tried submitting a ticket with them? I've always had good luck with their support.
I just got an update from my client. Before, I couldn't drop the database, because of the data in it. At the end, we decided to drop the database since the data can be added back (it's dev server, doesn't matter if we loose some dummy data).
I didn't find a solution for the problem above, but promoting HEROKU_POSTGRESQL_BRONZE_URL and restoring from backup solved the issue about not being able to access the database.
I've just deployed my application to heroku and pointed my custom domain to the heroku servers. How can I check the records in my heroku database?
You can use heroku run rails console and look at your records with Model.all or any other method.
If you want to backup the database look at heroku PG backups, you then can import your database on your local machine and look at it there. Depending on your db adapter you could use sqlite browser for sqlite3 or phpmyadmin for MySQL.
I found a similar question like this and here is what #Chowlett says:
"You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table."
You can also type select * from tablename; to view the table contents.
How to view current database schema for Heroku app in Terminal?
heroku db:pull to pull your production DB locally to take a peek in it.
I'll give the method for connecting via a GUI tool
Run the following command to get the database credentials from Heroku:
heroku pg:credentials DATABASE_URL
Then you can use a GUI tool like PG Commander or PGAdmin to connect to the db
Heroku now has an add-on named PostgreSQL Studio (currently free & in beta) that would let you access your database from within the browser, without having to use CLI, much like PHP MyAdmin.
To attach this add-on to your application,
heroku addons:create pgstudio
Then go to the list of add-ons on Heroku, select PostgreSQL Studio, authorize it, select the database to connect with from the dropdown list of all databases and it will take you to the web-based interface to handle your selected database.
You may refer to this official article on Heroku:
https://devcenter.heroku.com/articles/pgstudio
The easy answer is:
heroku pg:info
You can also download a client side Postgres, like Postico, and using the information provided in that URL to enter password and database name etc, then you can create locally, just like phpMyAdmin.
I use the admin_data gem, works well in Heroku.
You can use heroku dataclips that allows to run queries online. Here you can find documentation https://devcenter.heroku.com/articles/dataclips.
Connect to the database using Sequel Pro. You can find your ClearDB url using heroku config command. The structure for connecting is as follows:
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true
I've been using mysql forever. never really needed anything fancier. But I'm using heroku a lot and while I'm working, I like free search, so I'm using the acts_as_tsearch plugin. If you go to the git repository, it tells you:
* Preparing your PostgreSQL database
Add a text search configuration 'default':
CREATE TEXT SEARCH CONFIGURATION public.default ( COPY = pg_catalog.english )
So guess what? I
changed from mysql to postgresql in my rails config
ran that "CREATE TEXT" code in the sql pain of pgAdmin (a gui for postgres)
noticed that now my development DB has something called an "FTS configuration"
tried the search functionality and it works GREAT
But I'm having trouble getting that configuration to show up in the schema. When I did rake db:dump it doesn't make it in there. I know I can add this line to the schema.rb:
execute 'CREATE TEXT SEARCH CONFIGURATION public.default ( COPY = pg_catalog.english )'
and that works, but how can I get that configuration into the schema without my having to hand-add it? Can I create a file that is also loaded after schema.rb when someone types rake db:load?
And for the postgres people, a question: What does that CREATE TEXT SEARCH CONFIGURATION... do?
Why don't you try adding it to a migration file and rake that against the heroku db?