How connect deployed rails app to exist Heroku database - ruby-on-rails

I launched my Rails application on Heroku.
And I want to connect it to the database of an existing project of my friend, which is also deployed on Heroku. A friend gave me credentials and url to the database. However, I do not understand how to connect my project to my friend's database. I tried to change DABASE _URL but all is useless. Also, credential data was placed in database.yml and when I run the heroku run db:setup in the heroku terminal i get
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Couldn't create '*************' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege

Use following steps:
1) Add DATABASE_URL (complete url username, password, host) under settings / Config Vars.
2) For Rails apps you should look to use db:schema:load, db:structure:load or db:migrate instead of db:setup.
here is more info about issue https://help.heroku.com/63D7ALXT/why-am-i-seeing-user-does-not-have-connect-privilege-error-with-heroku-postgres-on-review-apps
You can share a single Heroku Postgres database between multiple apps with the heroku addons:attach command
heroku addons:attach my-originating-app::DATABASE --app new_app
for more info check here https://devcenter.heroku.com/articles/heroku-postgresql#sharing-heroku-postgres-between-applications
hope this helps

The reason you cannot update DATABASE_URL is because it's currently pointing at another database without any auxiliary attachments. This is a safety mechanism to prevent users from accidentally shooting themselves in the foot. This should allow for your changes.
heroku addons:attach <addon name, eg postgresql-cloudy-12345> --as ORIGINAL_DB --app <app name>
This will create an additional attachment name so you don't lose reference to the original database
heroku addons:detach DATABASE_URL --app <app name>
This will remove the Heroku-managed DATABASE_URL config var.
heroku config:set DATABASE_URL=<the connection string for the other database> --app <app name>
This sets DATABASE_URL to point at the other database which should put things into an operable state.*
* This caveat here is that this is a brittle configuration because hostnames can change for databases. If the db undergoes maintenance you will need to manually change the config var (ie repeat step 3).

I solved this situation by going to heroku.com to my app then setting then Config Vars
The DATABASE_URL was different than the ORIGINAL_DB_URL so I changed the DATABASE_URL to be similar to ORIGINAL_DB_URL
In my case: postgres://quxhxkemlsfzzv:964c7..........

Related

Unable to CREATE DATABASE postgresql on Heroku Hobby database

I connected to Heroku postgresql DB (Hobby env):
heroku pg:psql --app
The prompt shows :
appname::DATABASE=>
Error :
appname::DATABASE=> CREATE DATABASE hello ENCODING 'UTF-8';
ERROR: permission denied to create database
Any help regarding this will be appreciated.
Creating a database is disallowed because it's unnecessary. When you provision a Heroku Postgres add-on, the database and user are created on your behalf. You can see the information by running heroku config and checking the database configuration string.

Change heroku environment variables

I have an app up and running on heroku connected to it's own database, however i have a database in my own server which is free and I want to connect my app to it, I changed my Gemfile accordingly but it seems to just ignore it.
I tried
heroku config:add DATABASE_URL=url
but it said he couldn't replace the existing DATABASE_URL...
You can either do this via the command line by first removing and then adding the variable:
heroku config:remove DATABASE_URL
heroku config:add DATABASE_URL=http://someurl.com
or you can do this via the heroku dashboard.
Select your app
Go to Settings
Reveal config variables
Change your DATABASE_URL

How to change DATABASE_URL for a heroku application

I wanted to use an external Database with my heroku application. But I'm unable to edit the configuration cariables. I tried using GUI, Which says, Cannot overwrite attachment values DATABASE_URL. While I tried using CLI as well. I used the command: heroku config:addDATABASE_URL="postgresql://username:password#IP:PORT". However, this throws an error ... is not a heroku command.
After trying out most these answers, I came across an update in 2016, here:
the database needs to be detached first, then update the variable of the DATABASE_URL.
heroku addons:attach heroku-postgresql -a <app_name> --as HEROKU_DATABASE
heroku addons:detach DATABASE -a <app_name>
heroku config:add DATABASE_URL=
An alternative method which does not require detaching (which may not be a desired outcome of the switch) is to simply attach the new database and then promote it, which the Heroku Documents explicitly states as a way to set the DATABASE_URL.
heroku addons:attach heroku-postgresql -a <app_name>
heroku pg:promote heroku-postgresql -a <app_name>
I got the very same situation today when I need to change postgres to postgis. Detach doesn't work for me so I done this to database.yml:
production:
url: <%= ENV['DATABASE_URL'].sub(/^postgres/, "postgis") %>
https://github.com/rgeo/activerecord-postgis-adapter/issues/214.
SQLAlchemy 1.4.x has removed support for the postgres:// URI scheme, which is used by Heroku Postgres (https://github.com/sqlalchemy/sqlalchemy/issues/6083). To maintain compatibility, perform the following before setting up connections with SQLAlchemy:
import os
import re
uri = os.getenv("DATABASE_URL") # or other relevant config var
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
# rest of connection code using the connection string `uri`
This will allow you to connect to Heroku Postgres services using SQLAlchemy >= 1.4.x
As explained in this article, the correct syntax to set/add a configuration variable is
$ heroku config:set DATABASE_URL="postgresql://username:password#IP:PORT"
However, it looks like (see the comments) the DATABASE_URL has been deprecated and trying to update it will trigger an error.
Based on the Heroku docs this is how you would share a database with multiple apps.
heroku addons:attach my-originating-app::DATABASE --app sushi
Solved it. Just for the reference of the users who have the same issue or want to have a similar implementation. Here's the workaround which worked for me.
Heroku no more overwrites databse.yml, so I just modified the DATBASE_URL in the database.yml and pushed it :)
It worked too!
Source
In my case, I needed to launch an java spring boot application with my personal data base (postgres). I have an instance on AWS, and when loading the app, an error was occurring because it would connect without ssl.
Considering this documentation (https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-ssl-with-postgresql), it says:
We used to suggest adding the URL parameter sslmode=disable to JDBC URLs. We now require use of SSL for all new Heroku Postgres databases. We will be enforcing use of SSL on all Heroku Postgres databases from March 2018. Please do not disable SSL for your database or your applications may break.
So, resuming, step 1, I deleted my addon Heroku Postgres on Resources tab.
Step 2, I changed my application.yml
from:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://<url>:<port>/<dataBaseName>?createDatabaseIfNotExist=true&useSSL=false
username: <user>
password: <pass>
to
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://<url>:<port>/<dataBaseName>?createDatabaseIfNotExist=true&useSSL=false&sslmode=disable
username: <user>
password: <pass>
I added "&sslmode=disable" at the end of url string.
And finally, rebuild/deploy (which in my case is automatic after pushing into my repo on github).
I hope this would help someone.
Peace...
One way to edit the DATABASE_URL will be to create another app and add the heroku_postgres add-on there and then grab the url of that database and use that in your main app by configuring the environment variables and set the value of DATABASE_URL to that url of the database.
Now you can easily change the DATABASE_URL as that is not attached with the app.

Heroku remote database must be empty error after pg:reset

I have an app on Heroku and I'm trying to replace the database on Heroku with the database on my local machine.
I was under the impression that
heroku pg:reset DATABASE
would clear the remote database and allow me to push. However, after running pg:reset, I run
heroku pg:push
and I stil get this error:
Usage: heroku pg:push <LOCAL_SOURCE_DATABASE> <REMOTE_TARGET_DATABASE>
push from LOCAL_SOURCE_DATABASE to REMOTE_TARGET_DATABASE
REMOTE_TARGET_DATABASE must be empty.
Couple of questions:
1. do I have to specify local db and remote db? [I am in the root directory of my application on my local machine]
2. if so, how do I find what my local database name is? I know that heroku can divine the remote database via DATABASE_URL?
You have to specify BOTH the local database(where the data is coming from) and the remote database(where the data is going). The part about REMOTE_TARGET_DATABASE must be empty. is just a reminder to make sure you clear the Heroku database before you push.
You should be able to find your database from your database.yml file.
You can also find the heroku database by heroku pg:info
Reminder: Heroku runs PostgreSQL so you should be using PostgreSQL locally as well.

Check db status through DATABASE_URL

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.

Resources