How do I list all tables in all schemas owned by the current user in Postgresql?
I can list all tables in all schemas using
> dt *.*
but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.
I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:
https://stackoverflow.com/a/12902069
EDIT:
Based on the accepted answer, I've created the following View:
create view my_tables as
select table_catalog, table_schema, table_name, table_type
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema');
And now the following command gives me what I wanted:
select * from my_tables;
postgresql postgresql-9.1
add a comment |
I can list all tables in all schemas using
> dt *.*
but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.
I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:
https://stackoverflow.com/a/12902069
EDIT:
Based on the accepted answer, I've created the following View:
create view my_tables as
select table_catalog, table_schema, table_name, table_type
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema');
And now the following command gives me what I wanted:
select * from my_tables;
postgresql postgresql-9.1
add a comment |
I can list all tables in all schemas using
> dt *.*
but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.
I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:
https://stackoverflow.com/a/12902069
EDIT:
Based on the accepted answer, I've created the following View:
create view my_tables as
select table_catalog, table_schema, table_name, table_type
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema');
And now the following command gives me what I wanted:
select * from my_tables;
postgresql postgresql-9.1
I can list all tables in all schemas using
> dt *.*
but that also lists system tables that greatly outnumber my tables that I care about. I'd like all the tables (and possibly views) created by me in the public schema and any schemas I've defined.
I'm hoping to find a way to do this without having to explicitly add schemas to the search path as I create them as described here:
https://stackoverflow.com/a/12902069
EDIT:
Based on the accepted answer, I've created the following View:
create view my_tables as
select table_catalog, table_schema, table_name, table_type
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema');
And now the following command gives me what I wanted:
select * from my_tables;
postgresql postgresql-9.1
postgresql postgresql-9.1
edited May 23 '17 at 12:40
Community♦
1
1
asked Dec 9 '12 at 17:42
Peter GrovesPeter Groves
355247
355247
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
This will list all tables the current user has access to, not only those that are owned by the current user:
select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema')
and table_schema not like 'pg_toast%'
(I'm not entirely sure the not like 'pg_toast%'
is actually needed though.)
I you really need the owner information, you probably need to use pg_class
and related tables.
Edit: this is the query that includes the owner information:
select nsp.nspname as object_schema,
cls.relname as object_name,
rol.rolname as owner,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
and rol.rolname = current_user --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
Great answer, add awhen 'm' then 'MATERIALIZED_VIEW'
to show that new type.
– Forbesmyester
Apr 16 '18 at 9:26
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
add a comment |
The short answer to the question would be:
SELECT *
FROM pg_tables t
WHERE t.tableowner = current_user;
add a comment |
What is the best alternative in PostgreSQL for User_source in Oracle?
New contributor
add a comment |
See this. All tables:
SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f30061%2fhow-do-i-list-all-tables-in-all-schemas-owned-by-the-current-user-in-postgresql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This will list all tables the current user has access to, not only those that are owned by the current user:
select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema')
and table_schema not like 'pg_toast%'
(I'm not entirely sure the not like 'pg_toast%'
is actually needed though.)
I you really need the owner information, you probably need to use pg_class
and related tables.
Edit: this is the query that includes the owner information:
select nsp.nspname as object_schema,
cls.relname as object_name,
rol.rolname as owner,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
and rol.rolname = current_user --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
Great answer, add awhen 'm' then 'MATERIALIZED_VIEW'
to show that new type.
– Forbesmyester
Apr 16 '18 at 9:26
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
add a comment |
This will list all tables the current user has access to, not only those that are owned by the current user:
select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema')
and table_schema not like 'pg_toast%'
(I'm not entirely sure the not like 'pg_toast%'
is actually needed though.)
I you really need the owner information, you probably need to use pg_class
and related tables.
Edit: this is the query that includes the owner information:
select nsp.nspname as object_schema,
cls.relname as object_name,
rol.rolname as owner,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
and rol.rolname = current_user --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
Great answer, add awhen 'm' then 'MATERIALIZED_VIEW'
to show that new type.
– Forbesmyester
Apr 16 '18 at 9:26
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
add a comment |
This will list all tables the current user has access to, not only those that are owned by the current user:
select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema')
and table_schema not like 'pg_toast%'
(I'm not entirely sure the not like 'pg_toast%'
is actually needed though.)
I you really need the owner information, you probably need to use pg_class
and related tables.
Edit: this is the query that includes the owner information:
select nsp.nspname as object_schema,
cls.relname as object_name,
rol.rolname as owner,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
and rol.rolname = current_user --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
This will list all tables the current user has access to, not only those that are owned by the current user:
select *
from information_schema.tables
where table_schema not in ('pg_catalog', 'information_schema')
and table_schema not like 'pg_toast%'
(I'm not entirely sure the not like 'pg_toast%'
is actually needed though.)
I you really need the owner information, you probably need to use pg_class
and related tables.
Edit: this is the query that includes the owner information:
select nsp.nspname as object_schema,
cls.relname as object_name,
rol.rolname as owner,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as object_type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
and rol.rolname = current_user --- remove this if you want to see all objects
order by nsp.nspname, cls.relname;
edited Apr 16 '18 at 9:25
Community♦
1
1
answered Dec 9 '12 at 18:02
a_horse_with_no_namea_horse_with_no_name
39.6k775112
39.6k775112
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
Great answer, add awhen 'm' then 'MATERIALIZED_VIEW'
to show that new type.
– Forbesmyester
Apr 16 '18 at 9:26
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
add a comment |
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
Great answer, add awhen 'm' then 'MATERIALIZED_VIEW'
to show that new type.
– Forbesmyester
Apr 16 '18 at 9:26
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
This is good enough. I'm going to create a View called my_tables from this.
– Peter Groves
Dec 9 '12 at 20:24
Great answer, add a
when 'm' then 'MATERIALIZED_VIEW'
to show that new type.– Forbesmyester
Apr 16 '18 at 9:26
Great answer, add a
when 'm' then 'MATERIALIZED_VIEW'
to show that new type.– Forbesmyester
Apr 16 '18 at 9:26
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
While another answer is concise, this might be relevant when excluding namespaces.
– mlt
Feb 9 at 2:29
add a comment |
The short answer to the question would be:
SELECT *
FROM pg_tables t
WHERE t.tableowner = current_user;
add a comment |
The short answer to the question would be:
SELECT *
FROM pg_tables t
WHERE t.tableowner = current_user;
add a comment |
The short answer to the question would be:
SELECT *
FROM pg_tables t
WHERE t.tableowner = current_user;
The short answer to the question would be:
SELECT *
FROM pg_tables t
WHERE t.tableowner = current_user;
edited Aug 4 '15 at 13:14
dezso
22.1k116096
22.1k116096
answered Aug 4 '15 at 13:08
Sahap AsciSahap Asci
1,188412
1,188412
add a comment |
add a comment |
What is the best alternative in PostgreSQL for User_source in Oracle?
New contributor
add a comment |
What is the best alternative in PostgreSQL for User_source in Oracle?
New contributor
add a comment |
What is the best alternative in PostgreSQL for User_source in Oracle?
New contributor
What is the best alternative in PostgreSQL for User_source in Oracle?
New contributor
New contributor
answered 9 mins ago
vidhya sundarividhya sundari
1
1
New contributor
New contributor
add a comment |
add a comment |
See this. All tables:
SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';
add a comment |
See this. All tables:
SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';
add a comment |
See this. All tables:
SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';
See this. All tables:
SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r';
edited Aug 31 '16 at 5:13
Paul White♦
51.3k14278450
51.3k14278450
answered Aug 31 '16 at 0:20
ribafsribafs
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f30061%2fhow-do-i-list-all-tables-in-all-schemas-owned-by-the-current-user-in-postgresql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown