Postgres: Get owner of all schemas - sql

I want to find out what the owner is of my postgres database schema. I tried
select * from information_schema.schemata s order by schema_name
But when I use a visual database client for our postgres database, there appear to be more schemas than the results of the query above. How can I see owners of these 'hidden' tables? I suspect it has something to do with the owner of the schema not granting others to see the schema, but I just can't figure that out.

try:
select r.rolname as schema_owner, ns.* from pg_namespace ns join pg_roles r on ns.nspowner = r.oid

Related

Query to get schema names in Postgres

I need a logical / cleaner query to get all the schema names that I have created
select schema_name
from "information_schema"."schemata"
AND
SELECT nspname
FROM pg_catalog.pg_namespace;
Returns a superset of schema names that have system created schema which I don't need.
I just want the schema names that I have created.
Try filtering from information_schema.schemata the pg_* schemas and the information_schema itself:
SELECT * from information_schema.schemata
WHERE NOT schema_name LIKE 'pg_%' AND schema_name <> 'information_schema'
Excluding the schemas owned by postgres, as suggested by Gordon, works only with the condition that postgres isn't the owner of any other schema but the system schemas. In case you created a schema with the user postgres, it will be excluded from the result set.
If you don't want the built-in schemas, then use:
select schema_name
from "information_schema"."schemata"
where schema_owner <> 'postgres';
If you want the ones that you specifically created, then check for the ones where you are the owner.
select
*,
nspowner::regrole as role_name
from pg_namespace
where nspowner = 'postgres'::regrole;
returns all schemes where owner is postgres user. Change it to your user name to get schemes owned by you.
https://www.postgresql.org/docs/current/datatype-oid.html
Below query solved my problem
SELECT distinct pg_namespace.nspname
FROM pg_catalog.pg_inherits INNER JOIN
pg_catalog.pg_class ON (pg_inherits.inhrelid = pg_class.oid) INNER JOIN
pg_catalog.pg_namespace ON (pg_class.relnamespace = pg_namespace.oid)
WHERE pg_namespace.nspname <> 'public';

Check foreign table owner on Postgres

I have a foreign table on Postgres, linking to another server. I'm trying to select on this table, but I'm getting this error:
ERROR: permission denied for relation whitelist
Anyone knows how to check the ownership of a foreign table?
I've tried this, but it returns info just about local tables...
select t.table_name, t.table_type, c.relname, c.relowner, u.usename
from information_schema.tables t
join pg_catalog.pg_class c on (t.table_name = c.relname)
join pg_catalog.pg_user u on (c.relowner = u.usesysid)
where t.table_schema='schema_name';
Any admin tool should be able to tell you who owns a foreign table (or any other database object, for that matter). In pgAdmin, the owner appears in the Properties tab when you select the foreign table in the object browser, and psql will report the owner when you run \dE.
If you really need a query, this should do it:
SELECT
oid::regclass,
pg_get_userbyid(relowner)
FROM pg_class
WHERE relkind = 'f'

Translating query from Firebird to PostgreSQL

I have a Firebird query which I should rewrite into PostgreSQL code.
SELECT TRIM(RL.RDB$RELATION_NAME), TRIM(FR.RDB$FIELD_NAME), FS.RDB$FIELD_TYPE
FROM RDB$RELATIONS RL
LEFT OUTER JOIN RDB$RELATION_FIELDS FR ON FR.RDB$RELATION_NAME = RL.RDB$RELATION_NAME
LEFT OUTER JOIN RDB$FIELDS FS ON FS.RDB$FIELD_NAME = FR.RDB$FIELD_SOURCE
WHERE (RL.RDB$VIEW_BLR IS NULL)
ORDER BY RL.RDB$RELATION_NAME, FR.RDB$FIELD_NAME
I understand SQL, but have no idea, how to work with this system tables like RDB$RELATIONS etc. It would be really great if someone helped me with this, but even some links with this tables explanation will be OK.
This piece of query is in C++ code, and when I'm trying to do this :
pqxx::connection conn(serverAddress.str());
pqxx::work trans(conn);
pqxx::result res(trans.exec(/*there is this SQL query*/));//and there is a mistake
it writes that:
RDB$RELATIONS doesn't exist.
Postgres has another way of storing information about system content. This is called System Catalogs.
In Firebird your query basically returns a row for every column of a table in every schema with an additional Integer column that maps to a field datatype.
In Postgres using system tables in pg_catalog schema something similar can be achieved using this query:
SELECT
TRIM(c.relname) AS table_name, TRIM(a.attname) AS column_name, a.atttypid AS field_type
FROM pg_class c
LEFT JOIN pg_attribute a ON
c.oid = a.attrelid
AND a.attnum > 0 -- only ordinary columns, without system ones
WHERE c.relkind = 'r' -- only tables
ORDER BY 1,2
Above query does return system catalogs as well. If you'd like to exclude them you need to add another JOIN to pg_namespace and a where clause with pg_namespace.nspname <> 'pg_catalog', because this is the schema where system catalogs are stored.
If you'd also like to see datatype names instead of their representative numbers add a JOIN to pg_type.
Information schema consists of collection of views. In most cases you don't need the entire SQL query that stands behind the view, so using system tables will give you better performance. You can inspect views definition though, just to get you started on the tables and conditions used to form an output.
I think you are looking for the information_schema.
The tables are listed here: https://www.postgresql.org/docs/current/static/information-schema.html
So for example you can use:
select * from information_schema.tables;
select * from information_schema.columns;

Programmatically get all tables of a database owned by a user

I created the following query:
select
is_tables.table_name
from information_schema.tables is_tables
join pg_tables
on is_tables.table_name=pg_tables.tablename
where
is_tables.table_catalog='<mydatabase>'
and is_tables.table_schema<>'information_schema'
and is_tables.table_schema<>'pg_catalog'
and pg_tables.tableowner='<myuser>';
I assume there is no database vendor independent way of querying this. Is this the easiest/shortest SQL query to achieve what I want in PostgreSQL?
I think you're pretty close. Object owners don't seem to appear in the information_schema views, although I might have overlooked it.
select is_tables.table_schema,
is_tables.table_name
from information_schema.tables is_tables
inner join pg_tables
on is_tables.table_name = pg_tables.tablename
and is_tables.table_schema = pg_tables.schemaname
where is_tables.table_catalog = '<mydatabase>'
and is_tables.table_schema <> 'information_schema'
and is_tables.table_schema <> 'pg_catalog'
and pg_tables.tableowner = '<myuser>';
You need to join on both the table name and the schema name. Table names are unique within a schema; they're not unique within a database.

Get definition of function, sequence, type etc. in Postgresql with SQL query

I need the create scripts for PostgreSQL database objects.
I have not access to pg_dump. So I have to get everything with SQL queries. How could I do this?
To get the definition of a function use pg_get_functiondef():
select pg_get_functiondef(oid)
from pg_proc
where proname = 'foo';
There are similar functions to retrieve the definition of an index, a view, a rule and so on. For details see the manual: http://www.postgresql.org/docs/current/static/functions-info.html
Getting the definition of a user type is a bit more tricky. You will need to query information_schema.attributes for that:
select attribute_name, data_type
from information_schema.attributes
where udt_schema = 'public'
and udt_name = 'footype'
order by ordinal_position;
From that you need to re-assemble the create type statement.
For more details you will need to read through the documentation of the system catalog: http://www.postgresql.org/docs/current/static/catalogs.html
But you should prefer information_schema views if they return the same information.
You will find psql -E instrumental in your quest for those queries.
It displays the queries psql uses when executing its backslash-commands - like \df+ myfunc for details about this function.
Here is a complete sample query using pg_get_functiondef:
WITH funcs AS (
SELECT
n.nspname AS schema
,proname AS sproc_name
,proargnames AS arg_names
,t.typname AS return_type
,d.description
,pg_get_functiondef(p.oid) as definition
FROM pg_proc p
JOIN pg_type t on p.prorettype = t.oid
JOIN pg_description d on p.oid = d.objoid
JOIN pg_namespace n on n.oid = p.pronamespace
WHERE n.nspname = 'some_schema_name_here'
)
SELECT *
FROM funcs
;;
Note, you should obviously specify the schema name, (or "public" if you are using that schema)
You can also do \sf to see the user defined function in psql