1

I have an issue. I have database backup for existing DB let's call it dev_db. I recovered it but under a different name like master_db and backup how I understood has only tables, columns types and etc but it hasn't system information and tables like pg_constrains table and etc that's why I lost all my PK.

So, my first question can I update all tables in my database with PK for "id" column, is it enough? (if yes how to perform that) And my second question is how can I create a full backup of the necessary database with all system information?

asked Jun 28, 2016 at 6:14

1 Answer 1

2

You can generate the SQL to add the primary key with:

select concat('ALTER TABLE ',table_schema,'.',table_name,' ADD CONSTRAINT ',
 table_schema,'.pk_',table_name,' PRIMARY KEY (id)')
from information_schema.columns
where column_name ='id'
and table_schema='xxx';

Replace the xxx with the schema for which you want to create the primary keys. You only need to write the output to a file and check and execute it.

answered Jun 28, 2016 at 7:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.