Return name of the Postgres database containing value in a row in a table - postgresql

I am looking for some implementation to essentially search the entire Postgres instance, all of it's databases to find the database containing a specific row in a specific table.
So I have a Postgres instance with 170 databases, every database has the exact same schema and table layout. In each database there is a specific table "SM_Project" and specific row "ProjectName" in said table. We have instances where we know a ProjectName or at least a partial match (can use LIKE with %) but we have no idea which database it lives in.
I am wanting to script, simplify the ability to enter a ProjectName and search the entire Postgres instance (all databases in there) and return the name of the db that contains that record.
I foolishly thought this would be a simple task, with my lack of experience I've tried to do this with several SELECT statements and while I can explicitly connect to a database and search for the record from there, I can't find a way to return the parent database name. I was thinking ti clunkily script it in bash to iterate through the databases until we get a true return on a EXISTS in a SELECT statement. But I feel like I'm overlooking something fundamental.
So my setup is like this"
Postgres
db1
SM_Project
ProjectName
db2
SM_Project
ProjectName
db3
SM_Project
ProjectName
In short I'm looking to return the name of the database that contains a record of ProjectName equal to a string.
Any thoughts would be very welcomed!

Related

Backup file and query issue

Implement SQL script that find the differences between the contents of a relational table BOOK and a relational table with the same name as _DOC.
The script must first list the rows added to the relational table BOOK after the backup file was created, and finally list the rows changed in the relational table BOOK after the backup file was created.
In brief, the script must first list all added rows, then all deleted rows, and finally all changed rows in a relational table BOOK. It is allowed to use more than one SELECT statement to implement this task.
Created data as the backup file and unable to query the issue

How to get a history of latest postgres table writes regardless of which table it is

Assume I don't know which tables have been written to (not queries, I mean writes). Can I find out names of tables that were last written to?
Don't want constant reporting. Just a query I can run after testing newly added code.
Once I know the names, I'm set; I can just query them using normal sql and see the records. But need to know which tables in a 200 table database. Something like:
select names of last 10 tables that have been written to

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.

Backup a table using pg_dump

I am new to PostgreSQL. I have a database name employee (id , name, address , Phonenumber , salary). I would like to make a backup of the employee details if anyone of Phno,addres and salary is changed.
Is there any way of doing it using pg_dump or I should be satisfied with trigger method that output original Tuples onto another Table say Backup if any changes are made .
Please , if someone could elaborate in detailed manner how to get start with this using pg_dump.
pg_dump scripts out the current state of the database. That's all it does, with some fine-tuning to let you get at individual tables, schemas, etc. It does not watch for changes, it does not work at the row level (barring some zany row-level security setup), and it is not an audit log.
What you're describing -- backing up individual rows when they're modified -- is an audit log, so pg_dump is the wrong tool for the job. An update trigger which inserts the original row into an audit table is the canonical way to accomplish this, so you're on the right track there. If you need to generate scripts of the audit table, that's where pg_dump comes in.

Select query on multiple databases

What I am trying to do is verify a URL. I just need to be able to select that single value from all databases that we have currently in SQL Server 2008. All the databases are the same, just multiple instances of the same database for different users.
I am looking to pull one item from one table in each database.
Each database contains a table SETTINGS and within that table a value for MapIconURL. I need that value from each table from within each database. I am looking at around 30 or so databases that would have this value.
So I found the "undocumented" Stored Proc sp_MsForEachDb and have working....to a point.
The code I am using is this:
EXEC sp_MsForEachDb 'use ?; SELECT "?" as databasename,SETTINGSKEYID, SECTION, NAME, INIVALUE, DESCRIPTION
FROM ?.dbo.SETTINGS
WHERE [NAME] = "MapIconURL"'
I have noticed that it is not selecting all the databases, but that it is also selecting the master table as well as other system tables, and am thinking that may be why it is not selecting all tables. Is there a way to exclude the system related tables?
If the number (and name) of the databases is fixed, then you could simply do:
SELECT MapIconUrl FROM db1.dbo.SETTINGS
UNION ALL
SELECT MapIconUrl FROM db2.dbo.SETTINGS
...
However, if either the number or names of the databases is not fixed, then you have to build the query dynamically.
First, run a query such as SELECT name FROM master..sysdatabases to get the names of the databases.
Then, loop over the result set (in T-SQL, use a CURSOR) and build your query and execute it (in T-SQL, use sp_executesql).