How can I attach a database to an app in Heroku? - database

I'm using Heroku's Postgres addon, and I created a new production database from the Heroku Postgres addon page.
I Didn't add it directly to my App using the Resources page of my App.
Now I want to attach this database to my App so it'll be recognized by the heroku pg command.
I'm able to use the database btw after setting the DATABASE_URL config var of my app to point to it, but heroku pg command doesn't recognize it yet.
Additional info: The previous database was Shared, and the new one is a Production.

Heroku add-ons may now be attached across applications and multiple times on a single app.
heroku addons:attach ADDON_NAME -a APP_NAME
Source: https://devcenter.heroku.com/changelog-items/646
To know the name of your addon, do:
heroku addons
Source: https://devcenter.heroku.com/articles/managing-add-ons

Did you add the database using the app-independent https://postgres.heroku.com/ site? Or did you just create a postgresql database in your Heroku control panel?
If you created your database on https://postgres.heroku.com/, you will not see the database via your heroku pg:info command. What you can do to add your database to your application, however, would be to:
Log into https://postgres.heroku.com/.
Click on the database you want to attach to your application.
Under 'Connection Settings', click the configuration button at the top right.
Then click the 'URL' option.
Copy your database URL, this should be something like "postgres://blah:blah#ec2-23-23-122-88.compute-1.amazonaws.com:5432/omg".
In your application, on the command line, run heroku config:set DATABASE_URL=postgres://blah:blah#ec2-23-23-122-88.compute-1.amazonaws.com:5432/omg
What we did there was assign your database to the DATABASE_URL environment variable in your application. This is the variable that's used by default when you provision databases locally to your application, so theoretically, assigning this value should work just fine for you.

To get your database that you created at https://postgres.heroku.com/ attached to your actual heroku app that you are working on you can't use any of the pg backup commands and as far as I can tell there is no supported Heroku way of attaching a database to a heroku app.
You can however create a backup of your database using pg_dump and then use pg_restore to populate your new database that is attached to your app:
pg_dump -i -h hostname -p 5432 -U username -F c -b -v -f "backup-filename" database_name
Once that is complete you can populate your new database with:
pg_restore -i -h new_hostname -p 5432 -U new_username -d new_database_name -v "same_backup_filename"
Even if you are upgrading from the "basic plan" to a the "crane plan" you still have to do a backup and restore, but since the db's are already attached to your app you have the advantage of using the heroku backup commands.

Related

Error getting alerts from compliances Your environment may not have any index with Wazuh's alerts

I am new in elasticsearch. I have to set up wazuh with elasticsearch cluster. I did all the thing. I have also installed wazuh plugin on the Kibana . Once, I opened the app and clicked on the agent section It is saying =>
Error getting alerts from compliances
Your environment may not have any index with Wazuh's alerts
Please help me.
you could try to uninstall and install it again. Here you have the official uninstallation guide Uninstalling Wazuh with Elastic Stack, and after installing Elastic again with this guide Unattended installation
Remember if you want to preserve your configuration you can backup the files:
cp -p /var/ossec/etc/ossec.conf.orig /var/ossec_backup/etc/ossec.conf
cp -p /var/ossec/etc/local_internal_options.conf /var/ossec_backup /etc/local_internal_options.conf
cp -p /var/ossec/etc/ /var/ossec_backup/etc/client.keys
cp -p /var/ossec/queue/rids/ /var/ossec_backup/queue/rids/*
Here you have more information about that Migrating OSSEC agent
After that, you have to put the files in their original path again

Can Docker Containers maintain state between restarts?

Should containers be able to maintain state?
I am using a SQLServer Image like so.
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d microsoft/mssql-server-linux:2017-latest
Then I create a database in it using dotnet ef.
dotnet ef database update -v
Database works fine until I restart the container. At which point my database is gona and the container is reset to it's initial state.
What am I missing? Do containers not persist state?
If so what's the point in using them for databases?
Yes they can if you don not delete the container so you can
docker stop xxx
or just simply restart your machine and than use
docker start xxx
or
docker restart xxx
if you use docker run you create a new container so there is no previous state to talk about. For sql server specifically there is an option to create a volume and store data there. If you do that you can delete a container and recreate it again without loosing data as its is no longer stored inside it.

DB reset after deploy to meteor servers

I re-deployed my app to meteor by using 'meteor deploy '
and my Database was reset.
Any clue why this happened or how I can avoid it in the future ?
When a meteor app is deployed, your data saved in local mongo would not deployed to the server. So you could use mongodump and mongorestore to solve it:(docs)
Now first dump your database somewhere
mongodump --host localhost:3001
Get your mongodb`s credentials by running (in your app dir):
meteor mongo myapp.meteor.com --url
This will give you database url in the form:
mongodb://username:password#host:port/databasename
With these info you could fill them into mongorestore (docs) and restore your local database over
mongorestore -u username -p password -h host:port -d databasename ~/desktop/location_of_your_mongodb_dump
All of your data would get transferred in this way. I wish it could help.

Postgres copy Heroku Production DB to local development DB

I have a heroku database, d76mj7ltuqs.
I then have a local database, test_development.
The schema is the same on both of these databases - I want to pull all of the data from my production database and overwrite my local database, so that local is an exact replica of production at the time of pull.
How can I do that in Postgres?
Use heroku's "pg:pull":
You'll need to clear your local DB:
rake db:drop
Then collect some information from Heroku:
heroku pg:pull DATABASE_URL test_development
This will connect to the heroku DB, and copy it to the local database.
See Heroku's documentation on pg:pull for more details.
This command should do the work:
heroku pg:pull DATABASE_URL database-name --app heroku-app-name
clean your local database:
rake db:schema:load
dump your heroku database:
heroku pg:backups:capture -r <**your production git repo name**>
heroku pg:backups:download -r <**your production git repo name**>
load data in your local database
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d <**test database name**> latest.dump
this is how i do it, be sure to gzip it as your database grows. also don't export the ACL as you likely don't have the same postgres user on heroku and local accounts. replace with your specific details.
pg_dump -h ec2-##-##-##-##.compute-1.amazonaws.com -p <port> -Fc --no-acl --no-owner -o -U <username> <databasename> | gzip > dumpfile.gz
#<Prompt for Password>
gunzip -c dumpfile.gz | pg_restore --verbose --clean --no-acl --no-owner -d test_development -U <local_username>
Use your terminal to make a local pg_dump and then either psql or pg_restore it into your local database.
Similar method can be found here.
If this is a Rails app, you can use the following script to overwrite your local database with the latest dump you've generated on Heroku. If you uncomment the line with heroku pg:backups capture, the script will generate a new snapshot on Heroku before downloading it to your machine.
Note that you shouldn't have to edit the script, since it reads all the configuration from your database.yml file.
#!/usr/bin/env ruby
require_relative '../config/environment'
# Uncomment the line below if you want to generate a new snapshot of the
# Heroku production database before downloading it to the local machine
# `heroku pg:backups capture`
database_dump_file_pathname = Tempfile.new('latest.dump').path
`heroku pg:backups:download --output #{database_dump_file_pathname}`
# Get database config fom database.yml file
database_config = YAML::load_file(Rails.root.join('config', 'database.yml'))
database_name = database_config['development']['database']
database_username = database_config['development']['username']
database_password = database_config['development']['password']
# Overwrite local database with dump
cmd_line_arguments = [
'--verbose',
'--clean',
'--no-acl',
'--no-owner',
'--host localhost',
"-U #{database_username}",
"-d #{database_name}",
database_dump_file_pathname
].join(' ')
`PGPASSWORD=#{database_password} pg_restore #{cmd_line_arguments}`
See the Heroku docs on downloading DB backups for details.

Remote database not empty

I'm trying to push my local database to Heroku using the following command:
heroku pg:push <local db name> DATABASE --app <heroku appname>
and I get:
env: psql: No such file or directory ! Remote database is not
empty. ! Please create a new database, or use heroku pg:reset
So I run heroku pg:reset which resets it but I then get the following when trying to push again therefore going around in circles:
env: psql: No such file or directory ! Remote database is not
empty. ! Please create a new database, or use heroku pg:reset
Any ideas on how to solve this?

Resources