Amazon RDS postgres Data wiped and can't connect to database? - postgresql

I created an RDS Postgres instance. I'm new to RDS.
db host:
demodb.xxxuxxvxxxxx.us-east-2.rds.amazonaws.com
db identifier:
demodb
Every tutorial says to connect with this URL:
jdbc:postgresql://demodb.xxxuxxvxxxxx.us-east-2.rds.amazonaws.com:5432/demodb
but every time I do I get this error-
FATAl: database "demodb" does not exist.
I am able to connect using this:
jdbc:postgresql://demodb.xxxuxxvxxxxx.us-east-2.rds.amazonaws.com:5432/postgres
Now, while I was excited to connect after I used SQL workbench to create tables and insert data into those tables, a few hours later all my tables and data were deleted/wiped/dropped. Why would this happen? and How can I prevent it from happening in the future?

FATAl: database "demodb" does not exist.
demodb is db instance identifier. It is NOT the name of your database inside of PostgreSQL.
By default RDS PostgreSQL does not create a database for you. It seems to me that you haven't created an actual database when you setup your RDS PostgreSQL.
To create a database at RDS creation there is an option called Initial database name where you should specify the name of the database you want. Otherwise, no database is created, which is a default behavior:

Related

Cannot drop db in postgres [duplicate]

I can't seem to reset my database while using docker compose. I've tried killing the server, killing just the database, and restarting the machine.
Anyone know the best way to clear out the development database?
Here's what I tried:
docker-compose run web rake db:reset
I am getting this error:
PG::ObjectInUse: ERROR: cannot drop the currently open database
: DROP DATABASE IF EXISTS "postgres"
Couldn't drop database 'postgres'
rake aborted!
I'm using the setup exactly as described by the docker-compose quickstart: https://docs.docker.com/compose/rails/
I have a rails container and a postgres container
You are using the wrong database.
The database postgres is normally not used for user data, but for administrative purposes. For example, if you want to drop a database, you have to be connected to a different database in the PostgreSQL database cluster to issue the SQL statement DROP DATABASE. Normally, the database postgres is used for that purpose, and I have no doubt that Docker does exactly that when it tries to drop a database.
If you really want to drop the database postgres, you'd have to connect to some other database in the cluster. The correct solution, however, is to keep your data in a different database. Then the problem should go away by itself.

Create Postgres User with admin rights just for one database

How can I create a postgres user who has admin access only to one database but cannot inspect or interfere with other databases in the postgres instance? The use case is I'm creating a multitenant application
where each tenant gets his own database in the postgresql instance and can create schemas, tables etc and
perhaps use a few pg_tables to inspect his own database but not others.
cannot change the name of the database as it's controlled by me
EDIT: Added more constraints
That's fairly trivial:
CREATE DATABASE newdb;
GRANT CREATE ON DATABASE newdb TO newdba;
Add pg_hba.conf entries to allow newdba to connect to newdb only.

How to shut down one database in a db2 instance?

I want to shut down one database in a db2 instance with multiple dbs.
I don't want to deactivate the db as it will reconnect when I try to connect. It should be completely shut down so I get a connection error when trying to connect to the db.
This is not a programming question so it can be viewed as off topic.
There are different techniques, each has advantages/disadvantages.
You can quiesce the database and later unquiesce it.
or you can revoke connect rights, and later grant them, but this depends on how well your role separation is done.
or you force off existing applications and then connect in exclusive mode as the instance owner (provided that your applications NEVER connect with instance-owner credentials).
One trick you could use is to temporarily recatalog the database you want to deactivate under a different name; this will prevent applications from connecting to it using the original name, regardless of the authority they use.
First, determine the database path by looking at its catalog entry:
db2 list db directory
The value of the "Local database directory" property is what you need.
Now you can recatalog the database:
db2 uncatalog db orig_db
db2 catalog db orig_db as foobar on <path>
where <path> is the local database directory determined previously.
Once you force all applications currently connected to the database in question you will be able to deactivate the database:
db2 list applications
db2 "force application (<app id 1>, <app id 2>,...)
db2 deactivate db foobar
Later on you can restore the catalog entry to its original value:
db2 uncatalog db foobar
db2 catalog db orig_db on <path>

How to rename an Amazon RDS hosted PostgreSQL database

I have restored a snapshot from an existing RDS PostgreSQL database. Now I want to rename that database but can't find how to do it anywhere in the AWS documentation.
Nor can I find how to use the master password (which I expect let's me do it).
Logging in with postgres into template1 database and then ALTER DATABASE foo RENAME TO bar; worked

Insert records to Heroku Postgres database using a rails script

I need to update a dababase from an other database... For this I need to search in existing database if the records exists, if not add... One of the database is locally stored in my computer, and the other one is on heroku... But I don't know how to access heroku database from my computer to create the inserts and to query if I have to insert or not...
Any idea how can I do something like this?
You can connect via any DB tool, e.g. Oracle SQL Developer with Postgres JDBC connector, to a Heroku Postgres DB. The parameters are listed in the GUI of postgres.heroku.com for your instance.