Group many postgresql databases into separate schemas into same database - postgresql

We have many postgresql databases with the same structure using only public shcema on each one.
How can I group all of them in a single database using separate schemas?

You can dump the database definition and data out, edit the output by putting the default schema as whatever you choose and run the scripts back into database.
Remember to make the dump in SQL format, pg_dump with default custom format won't work. The schema change will only need a change on a row like
SET search_path TO *whateverschema*
If you don't want to edit the dumps (maybe they're very large), you can of course also restore them one by one to the public schema, alter the tables into the desired schema and then repeat for the next one.
There is no special way to convert an existing database into a schema in another database unfortunately.

I forgot to post the answer afer all klin comment was the answer, this step was the solution,
Inside customer_x database:
alter schema public rename to customer_x;
And then take pg_dump customer_x:
pg_dump "customer_x" --schema "customer_x" -f customer_x.sql
Inside new conglomerated database:
DROP schema customer_x CASCADE;
create schema customer_x;
Then load the dump of customer_x:
psql "conglomerated_database" -f customer_x.sql

Related

Cannot drop a schema in PostgreSQL

I am trying to drop the schema masterdata from a postgres database, but it does not work. I am using PostgreSQL 9.5. I have 2 databses, one of them has the masterdata schema, which I want to drop. Here is the structure (in DBeaver):
My first attempt was just to execute the SQL statement DROP SCHEMA masterdata in DBeaver, but it tells me, that such a schema does not exist (but it does show it, as we can see in the picture!). Maybe, it does not know, in which of the 2 databses to look for this schema? If so, then how to specify it? I was looking for a way to specify the database, but did not find anything.
However, my second attempt was to use psql and to type the command
psql -U postgres -d bobd -h localhost
So here I concretely specify, which databse to use! psql does not answer anything, it just asks for the next command. And when I type \dn to view the current schemas, then the schema masterdata is still there! Also, the data in the tables is still there. I tried the same with other users instead of the user postgres (also with the owner of the schema to remove), but the result is the same.
Any ideas, what I am doing wrong?
Your DBeaver session was probably connected to the wrong database (postgres?).
Do it from the psql session, that is easiest.
Right after \dn showed you that there is indeed such a schema, enter
DROP SCHEMA masterdata;
It may complain that there are objects in that schema. Then you can use
DROP SCHEMA masterdata CASCADE;
Maybe your schema name contains characters that do not display or display differently (maybe capital letters) in DBeaver. I suggest you check names by running following query:
SELECT '~~' || nspname || '~~' FROM pg_catalog.pg_namespace;
Try adding quotes around schema name if you have added capital letters when creating schema.

How to copy all my tables from a Postgres db to another one without pg_dump?

I need to copy all my tables from my Postgres database to another one. Usually I'd do it with pg_dump and then pg_restore, but unfortunately I don't have rds_superuser permissions and so it doesn't work.
Basically I have to copy the data table by table, create table in the other db and then import the data. This is how I would do if I'd have to do it manually.
Does anybody know how to do this in a programmatic way?
Thanks!
If you have createdb privilege, you can try using something like below.
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER your_user_name;
It will copy all your tables(with structure) and data to your new db from your old one.

restoring database from pg_dump file creates strange tables

I have backup created like this:
pg_dump dbname > file
I am trying to restore the database (after drop database and create database) like this:
psql dbname < file
What I get is a database full of tables that are created with dbname.tablename instead of just tablename.
How do I restore a postgres database making sure the tables it creates has just tablename and not dbname.tablename?
Thanks to #Craig Ringer for pointing me in the right direction.
Yes, there was SET search_path on the database for the original DB. This created the table names with schema names prefixed to table names.
Removing or commenting those out of the backup script created tables without a schema prefix. Which was desirable. But the restore didn't result in complete restore, and many tables got left out.
So did the restore, with usual means. Tables are created with schema names prefixed. The sql query scripts broke because they were not specifying the schema names every time they queried the table. To fix this, I followed this - https://stackoverflow.com/a/2875705/1945517
ALTER ROLE <your_login_role> SET search_path TO dbname;
This fixed the broken queries.

Restore Particular tables in one schema to another schema postgresql

In my case, i have backup file in full database. Now I want to restore some particulars tables in public schema.
Those tables are already stored in another schema. Any Feasible solution is there? and How to do it
It's not clear whether the tables you want to restore were backed up from the schema "public", or whether they were backed up from a different schema.
If your backup is in archive format, not in a plain text format, you can restore individual tables (see the -n and -t options to pg_restore). As far as I know, you can't restore them to a different schema. Instead, you'd restore them to their original schema, then move each table with ALTER TABLE table_name SET SCHEMA new_schema;.
Since you already have tables of the same name in the target schema, I expect you'll have to rename them before restoring from backup. After you restore from backup, and you move the restored tables to the schema "public", you can rename those tables to their original names. PostgreSQL understands that public.table_name and new_schema.table_name are different tables.

How to delete database tables in postgres

I have created some databases with postgres, and put some data in them.
The problem is when I delete/drop database, and then create new database, the new database always contains tables and data from the first database that was created with postgres.
How can I delete database so that, when the new database is created it dosent contain data from old database?
Thanks
It sounds like you created tables in the template1 database (or you specify the TEMPLATE xyz option with your CREATE DATABASE statement).
To get rid of the tables in the template1 database, connect to it and drop all tables there. After that new database will not contain those tables any more.
Delete the rows in table using truncate commnad
truncate <tablename>
then delete database using drop command
drop database <databasename>