Migrate postgresql schema AWS RDS - postgresql

Ok, I'm a mobile developer trying to learn backend development.
I have two AWS RDS instances running postgresql. One for development and one for production.
The scenario is that the development data base is operated on. Create tables, add postgis, new relationships, what ever.
Now, I want to go put all those schema changes into the production database. Obviously I don't want to migrate development data to production, just the schema and db changes.
What do I do?

You can dump schema only:
pg_dump -s databasename > your_schema.sql
Then you restore the dumped schema from your_schema.sql by running on the production sever:
psql < your_schema.sql
Use user/pass options as needed.

Related

How I can change data in prisma.schema, without losing data after migration?

When I changed prisma.schema and run:
npx prisma migrate dev --name init
My Postgres lost all data. Can I run another command which doesn't remove my data? What is best for me in this situation?
From the prisma.io documentation:
Prisma Migrate is an imperative database schema migration tool that enables you to keep your database schema in sync with your Prisma schema as it evolves and
main existing data in your database
Prisma Migrate generates a history of .sql migration files, and plays a role in both development and deployment.
This means that the prisma migrate command is supposed to be syncing the data from the model with the one in the database, adding/removing rows if needed, etc.
Perhaps the command you are looking for is prisma db push? They both have similar uses and aspects, one is for dev, one isn't.

Copy data from Postgres DB (GCP Project A) to another Postgres DB (GCP Project B)

I would be happy to get your help / feedback re data load.
Goal:
Load source data from a Postgres database, which is located in GCP project A to another Postgres database, which is located in GCP project B.
Challenge:
Get a connection (I have an IAM account with sufficient rights to run a COPY TO / COPY FROM command) to the Postgres DB in GCP Project A and copy the table either to a CSV or create a dump that can be used in order to be inserted to another Postgres DB in GCP Project B.
How do I connect to the database (e.g. if I create a key, where shall I store the json keyfile and would that approach even be feasible?) with this IAM email account?
Other ways I've researched were to use psycopg2 (thus I could use the function cursor.copy_expert (which doesn’t need any superuser right or Postgres user credentials and copy the data), but I didn’t succeed in connecting to the database with psycopg2 due to challenges with cloud proxy.
Another idea was to use pg_dump or gcloud sql export csv.
I would be curious if some of you were facing a similar challenge and how did you solve it and what might be the best way/practice
You can have a try out database migration service. You can set up a continuous migration configuration and use Cloud SQL for PostgreSQL.
Hello after a lot of searching I've come to these solutions:
If you have continuous copy, you need to use the database migration service, check this documentation.
If you have one shot copy:
you can restore your instance, see the bottom page of this documentation
you can create a bucket and backup your instance on it, then import it from the other project

Is it possible to have multiple databases per one heroku postgres plan?

Is it possible to have multiple databases per one Heroku postgreSQL plan(instance)?
Unlike Amazon RDS, Heroku doesn't allow creating multiple databases – your DB role simply doesn't have permissions to CREATE DATABASE ..;.
However, you can create several "apps", each one with its own Postgres and then use multiple Postgres DBs in single app (see for example https://devcenter.heroku.com/articles/heroku-postgresql#sharing-heroku-postgres-between-applications – this is a way to change "attached" database, but you can just add config vars pointing to multiple database credentials with heroku config and then use those credentials inside your app).
Alternatively, you can create Amazon RDS Postgres (one or as many as you wish) in the same Availability Zone as your Heroku app, and use this Postgres instance (or several) in your Heroku app.
Actually this is not completly true.
You are right, you do not have the permisson to create a database,
but instead it is possible to just add more Heroku Postgres databases as Resources.
This way you will have multiple plans(instances)

How I can copy my local PostgreSQL database to Heroku for SpringBoot app

I have deployed my SpringBoot app to Heroku. Now I would like to copy my local PostgreSQL to Heroku.
I have found some information on devcenter.heroku.com.
However I don't understand enough about the using of file db.changelog-master.yaml.
Could anyone give me details about the simplest solutions to copy the database?
Create a valid dump of your local postgres database and host it somewhere publicly available. Now you will be able to restore this entire dataset (schema and records) with pg:backups:restore as shown here. The sole caveat here is that the target database must be completely empty for this to work. You can empty a Heroku postgres database with heroku pg:reset.
If you cannot take the approach listed above then you can run pg_restore directly from your local instance, provided your local version of Postgres is >= the target version of Postgres. This also applies to creating the dumpfile and is a requirement because pg utilities are not guaranteed to be forward compatible. Documentation for pg_restore is here.

Postgres databases linked to separate Heroku environments

Using this article: Heroku dev environments
I successfully made two separate Heroku apps, one test, and one prod. This each have their own remotes on my local development box. But now I don't know how to separate the postgres tables of my now distinct development and prod applications.
This is the command I use to create a postgres table with my Heroku app:
heroku addons:add heroku-postgresql
But then I lost access to my original postgres DB. I would like this setup so I don't inadvertently hose my prod DB from my development branch.
TL;DR: How do i keep distinct postgres databases with a single Heroku application that has multiple environments?
EDIT #1: I found that I can call:
heroku addons:add heroku-postgresql:dev
multiple times to create several dev databases. What is the best practice for pointing my dev vs. prod apps to each database without having to hard code my database links like this:
//this function connects to the Heroku postgres db
function pg_connection_string(){
return "dbname=dcs1k5588jbfad host=ec2-54-243-224-162.compute-1.amazonaws.com port=5432 user=###user_name### password=######### sslmode=require";
}
Is there a Heroku-fung-shui of swapping database pointers?
You can set the environment variable or heroku config variable DATABASE_URL to anything you wish. This is what Heroku applications expect to be the default database for that application.
If you're using a production Database you can Fork from your production app to a test app to get a copy of the data as well: https://devcenter.heroku.com/articles/heroku-postgres-fork