Maintaining a development database with exactly the same schema - postgresql

I'm trying to run a different database for development as my initial product release is coming so I'd like to know how to maintain two different databases. I'm using postgresql as my DBMS
I want development database and production database to have exactly the same schema. Is there a way to do this automatically? If I have to to manually, what would be the best way to update schema?
thank you

I want development database and production database to have exactly the same schema.
Then just create 2 databases with the same schema(s).
Or you can read more about template databases - https://www.postgresql.org/docs/11/manage-ag-templatedbs.html.
The idea is that when you create new database, it's actually copied from template1, thereof you can edit template1, and every new database will have schemas/tables that you need.

Related

Recreate SQL Commands from db

I have created a db long ago using django. Now as we are migrating the application, so I need all the CREATE TABLE sql queries which django might have run to create the entire db for our service (which has around 70-80 tables and each table has avg 30-70 columns).
Both the servers old and new are using Postgres for databases.
But the technology stack is completely different (A 3rd party proprietary application which will host the service) instead of django.
If I start to write all the tables again from scratch, it will take at least a week or two.
Is there any way either from Postgres or from django which can generate the CREATE TABLE sql schema for an entire db keeping all the relationship as is?
Also, I have to do minor modification to that schema as per customer requirement.
p.s - pg_dump won't work as I need actual schema itself to get it reviewed from client.

sync normalize tables vs json table in postgresql, so i can use graphql and sql, future neo4j

I have not found real sync from postgreSql to new graph database like neo4j, so I've decide to use same postgresql to sync one normalise tables with json table on the same postgresql with a differenta name for a database. So i have the best of two worlds, sql and nosql database.
When i see sql is more fast than graphql i can choose, and in the future, when i moved the nosql tables to a real graph database like neo4j and i ll be able to sync, i dont need to change the app that can use both database synced
Someone did already this? or it's a dumm idea? Or someone already use automatic libraries to sync from postgresql to neo4j ? and the another sens too ? or must I write sync scripts from scratch if i want to sync two databases?

Create a dev database with same table structure as production in Postgres 9.3

I want to create a 'development' database for my web application.
I'm using Postgres 9.3, and I would like 'devdb' to have the exact table structure as
my production 'appdb'. I do not want them to share data, but I want devdb to receive any changes made to table structures, if this is possible. (ie. if i add a new table in appdb, I want devdb to also have the new table, same thing if I remove a column)
Do I need to use schemas for this, and if so, how? My appdb currently has a schema of public.
Thanks!
I think your best bet is to use:
pg_dump --schema-only prod | psql dev
To keep the schemas in sync, either drop and reload the dev db, or script your schema changes so you can apply the change to both DBs. You should be doing that anyway, testing changes in dev before applying them to production.
(Tools like Liquibase can be interesting for this).
Attempts to link DDL definitions directly are unsafe. They create a dependency from production to dev. That's risky.
For example, if you were to use a table inheritance based approach then a long-running transaction holding a lock on the dev tables might cause delays on production.

libpq code to create, list and delete databases (C++/VC++, PostgreSQL)

I am new to the PostgreSQL database. What my visual c++ application needs to do is to create multiple tables and add/retrieve data from them.
Each session of my application should create a new and distinct database. I can use the current date and time for a unique database name.
There should also be an option to delete all the databases.
I have worked out how to connect to a database, create tables, and add data to tables. I am not sure how to make a new database for each run or how to retrieve number and name of databases if user want to clear all databases.
Please help.
See the libpq examples in the documentation. The example program shows you how to list databases, and in general how to execute commands against the database. The example code there is trivial to adapt to creating and dropping databases.
Creating a database is a simple CREATE DATABASE SQL statement, same as any other libpq operation. You must connect to a temporary database (usually template1) to issue the CREATE DATABASE, then disconnect and make a new connection to the database you just created.
Rather than creating new databases, consider creating new schema instead. Much less hassle, since all you need to do is change the search_path or prefix your table references, you don't have to disconnect and reconnect to change schemas. See the documentation on schemas.
I question the wisdom of your design, though. It is rarely a good idea for applications to be creating and dropping databases (or tables, except temporary tables) as a normal part of their operation. Maybe if you elaborated on why you want to do this, we can come up with solutions that may be easier and/or perform better than your current approach.

Oracle Database 10g: Refresh Production Database Schemas to Stage Database with not all production data

Hi Oracle DBA Gurus, Is there an easy way to do to refresh production database schemas to stage database with smaller amount produciton data (not all production data)? Both databases are 10g r2.
For the schemas? Yes, there exists numerous solutions for this. For the data? I don't believe there is a generic way to do this. Since your data is unique for you, it's impossible to build a generic solution copying a subset of data, taking into account relationships, triggers, stored procedures etc.
Depending on what data you have, it might not be hard to do manually anyway. Once you have working solution, it can be very handy for a long time with small efforts in updating.