I need to write a script that will output data from a PostgreSQL database that I do not know the structure of. What query will return the names of all tables in a database? And what query will list the names of all columns in a table?
The database query tool psql, part of the PostgreSQL distribution, provides table description functionality.
# psql postgres postgres
psql (9.1.0)
Type "help" for help.
postgres=# -- list all tables:
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+----------
public | my_table | table | postgres
public | my_table2 | table | postgres
(2 rows)
postgres=# -- describe table:
postgres=# \d my_table
Table "public.my_table"
Column | Type | Modifiers
--------+---------+-----------
col1 | integer |
col2 | text |
The rest of the psql commands you can get with \?:
postgres=# \?
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\h [NAME] help on syntax of SQL commands, * for all commands
\q quit psql
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
Input/Output
\copy ... perform SQL COPY with data stream to the client host
\echo [STRING] write string to standard output
\i FILE execute commands from file
\o [FILE] send all query results to file or |pipe
\qecho [STRING] write string to query output stream (see \o)
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
\da[S] [PATTERN] list aggregates
\db[+] [PATTERN] list tablespaces
\dc[S] [PATTERN] list conversions
\dC [PATTERN] list casts
\dd[S] [PATTERN] show comments on objects
\ddp [PATTERN] list default privileges
\dD[S] [PATTERN] list domains
\det[+] [PATTERN] list foreign tables
\des[+] [PATTERN] list foreign servers
\deu[+] [PATTERN] list user mappings
\dew[+] [PATTERN] list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN] list text search configurations
\dFd[+] [PATTERN] list text search dictionaries
\dFp[+] [PATTERN] list text search parsers
\dFt[+] [PATTERN] list text search templates
\dg[+] [PATTERN] list roles
\di[S+] [PATTERN] list indexes
\dl list large objects, same as \lo_list
\dL[S+] [PATTERN] list procedural languages
\dn[S+] [PATTERN] list schemas
\do[S] [PATTERN] list operators
\dO[S+] [PATTERN] list collations
\dp [PATTERN] list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\ds[S+] [PATTERN] list sequences
\dt[S+] [PATTERN] list tables
\dT[S+] [PATTERN] list data types
\du[+] [PATTERN] list roles
\dv[S+] [PATTERN] list views
\dE[S+] [PATTERN] list foreign tables
\dx[+] [PATTERN] list extensions
\l[+] list all databases
\sf[+] FUNCNAME show a function's definition
\z [PATTERN] same as \dp
Formatting
\a toggle between unaligned and aligned output mode
\C [STRING] set table title, or unset if none
\f [STRING] show or set field separator for unaligned query output
\H toggle HTML output mode (currently off)
\pset NAME [VALUE] set table output option
(NAME := {format|border|expanded|fieldsep|footer|null|
numericlocale|recordsep|tuples_only|title|tableattr|pager})
\t [on|off] show only rows (currently off)
\T [STRING] set HTML <table> tag attributes, or unset if none
\x [on|off] toggle expanded output (currently off)
Connection
\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]
connect to new database (currently "postgres")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo display information about current connection
Operating System
\cd [DIR] change the current working directory
\timing [on|off] toggle timing of commands (currently off)
\! [COMMAND] execute command in shell or start interactive shell
Variables
\prompt [TEXT] NAME prompt user to set internal variable
\set [NAME [VALUE]] set internal variable, or list all if no parameters
\unset NAME unset (delete) internal variable
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID large object operations
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN
('pg_catalog', 'information_schema');
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'YourTablesName';
This page has some great information on retrieving information from information_schema: http://www.alberton.info/postgresql_meta_info.html
I know this is 5 years after the thread was started, but I'd like to add a slight variation to the solutions proposed so far, in case it helps someone else (ultimately, this is what I had to come up with).
The potential problem with the above, is that if implemented naively against a database with hundreds of tables and thousands of fields, a developer might first query the set of tables, and then inside a loop, query all fields for each table. That hammers the database server. I know nobody specifically suggested using a loop, but nobody warned against it either. And frankly, it's sort of implicit in the way the answers are structured, in that they effectively say "first query all tables, next query all fields". Translated to code, that process can't really be anything other than a loop.
A better way (imo) to accomplish the original question is to run a query like:
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE table_schema in ('a', 'b', 'c', 'd')
where a,b,c,d, ... are the schemas with tables that you care to articulate.
This provides you a dataset that isn't normalized, but that doesn't really matter since you're consuming it with an app anyway - it's trivial to parse the result at the app layer the way you need, and you've only hit the database server with one super lightweight query, instead of possibly hundreds inside a loop.
Anyway, hope that helps someone!
use the ANSI information_schema views
select * from information_schema.tables
select * from information_schema.columns
If you have access to psql, you can use \d and \d table.
In terms of SQL, first is equivalent to
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
second
SELECT column_name FROM information_schema.columns WHERE table_name ='table'
psql has the -E, --echo-hidden option, which will print out all queries made by psql commands.
For example:
psql -h localhost -U postgres -p 5432 postgres -E
postgres=# \d
******** QUERY *********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' WHEN 'I' THEN 'index' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
\d <table_name>
example :
\d authors
Don't write it yourself. Use phpPgAdmin - it will be much easier, faster and less error prone.
Related
I have a database with many tables that are used as a reference for valid values in the other tables. These reference tables are all named valid[table], so I can display them in psql with \dt valid*.
I'd like to be able to select all the non-reference tables (which are more variably named, but none of them start with valid), but can't figure out how. I've tried various things like \dt !valid* , \dt !~valid* , \dt NOT LIKE 'valid%', but these either error or don't find a match.
Any suggestions would be appreciated!
According to the manual, psql supports these patterns. Normally you could use regular expression negative lookahead (?!valid)* but ? cannot be used in regular expression with psql because it is translated to .. It is probably easier to do a query from INFROMATION SCHEMA:
SELECT table_name FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema') --exclude system tables
AND table_type = 'BASE TABLE' -- only tables
AND table_name NOT LIKE 'valid%';
I want to get data about all relationships from a PostgreSql-database with query like
SELECT
[creatorUser].[primTableName].[primColumnName] AS primary_column,
[creatorUser].[foreignTableName].[foreignColumnName] AS foreign_column,
FROM
sys.sysidx si,
sysidxcol sic,
sys.SYSFKCOL sfc,
sysusers su,
sys.systable st,
sys.syscolumn sc
WHERE
si.table_id=sic.table_id AND
si.index_id=sic.index_id AND
sfc.primary_column_id=sic.primary_column_id AND
sfc.foreign_table_id=sic.table_id AND
st.table_id=sfc.foreign_table_id AND
su.uid=st.creator AND
sc.column_name=si.index_name
Can anybody help, how to get that data? How to be a correct query
owner1.table1.primCol1 owner1.table1.foreignCol1
owner1.table1.primCol2 owner1.table1.foreignCol2
..
?
PostgreSQL has no sys tables, so it's not clear how you could possibly expect that query to work. Given your comments, I'm guessing that's the query you use on Sybase, and you want to translate it for PostgreSQL.
Well, assuming that the Sybase syntax:
[creatorUser].[primTableName].[primColumnName] AS primary_column,
[creatorUser].[foreignTableName].[foreignColumnName] AS foreign_column,
means "Concatenate the user who created the relationship, the table name, and the column name" I'd probably do something in PostgreSQL like:
SELECT
format('%I.%I.%I', fk.table_schema, fk.table_name, fk.column_name) AS foreign_side,
format('%I.%I.%I', pk.table_schema, pk.table_name, pk.column_name) AS target_side
FROM
information_schema.referential_constraints rc
INNER JOIN information_schema.key_column_usage fk
ON (rc.constraint_catalog = fk.constraint_catalog
AND rc.constraint_schema = fk.constraint_schema
AND rc.constraint_name = fk.constraint_name)
INNER JOIN information_schema.constraint_column_usage pk
ON (rc.unique_constraint_catalog = pk.constraint_catalog
AND rc.unique_constraint_schema = rc.constraint_schema
AND rc.unique_constraint_name = pk.constraint_name);
Except for the format(...) that'll work on Sybase too, with some luck.
Output like:
foreign_side | target_side
---------------------+---------------------------------
public.users.id_cat | public.usercategories.id_numcat
(1 row)
That's schema.table.column . I'm guessing Sybase's "creatorUser" is really "schema" in this case.
You should query appropriate tables under pg_catalog or information_schema schemas. The easiest way to figure out the correct query is to run (... = other command-line options like user, database, ...)
psql -E ...
and then psql displays meta-data queries used in internal commands like \d, \dt, \dv, ... From manual:
-E
--echo-hidden
Echo the actual queries generated by \d and other backslash commands. You can use this to study psql's internal operations. This is equivalent to setting the variable ECHO_HIDDEN from within psql.
information_schema is same as pg_catalog but more portalbe (it is defined in SQL standard). If your app uses postgres only then I would use pg_catalog instead of information_schema
Is there any query available to list all tables in my Postgres DB.
I tried out one query like:
SELECT table_name FROM information_schema.tables
WHERE table_schema='public'
But this query returns views also.
How can i get only table names only, not views?
What bout this query (based on the description from manual)?
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
If you want list of database
SELECT datname FROM pg_database WHERE datistemplate = false;
If you want list of tables from current pg installation of all databases
SELECT table_schema,table_name FROM information_schema.tables
ORDER BY table_schema,table_name;
Open up the postgres terminal with the databse you would like:
psql dbname (run this line in a terminal)
then, run this command in the postgres environment
\d
This will describe all tables by name. Basically a list of tables by name ascending.
Then you can try this to describe a table by fields:
\d tablename.
Hope this helps.
Try this:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public' AND table_type='BASE TABLE'
this one works!
SELECT table_name
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='public';
For MySQL you would need table_schema='dbName' and for MSSQL remove that condition.
Notice that "only those tables and views are shown that the current user has access to". Also, if you have access to many databases and want to limit the result to a certain database, you can achieve that by adding condition AND table_catalog='yourDatabase' (in PostgreSQL).
If you'd also like to get rid of the header showing row names and footer showing row count, you could either start the psql with command line option -t (short for --tuples-only) or you can toggle the setting in psql's command line by \t (short for \pset tuples_only). This could be useful for example when piping output to another command with \g [ |command ].
How about giving just \dt in psql? See https://www.postgresql.org/docs/current/static/app-psql.html.
select
relname as table
from
pg_stat_user_tables
where schemaname = 'public'
This will not work if track_activities is disabled
select
tablename as table
from
pg_tables
where schemaname = 'public'
Read more about pg_tables
I have a user in my DB2 database that I want to grant select rights on all tables and views for a given schema. Any thoughts on how to do that as one SQL statement?
In order to grant select to a given user, you have to "generate" the sentence for each table and view of a given schema. You can do it via the CLP with a query like this:
db2 -x "select 'grant select on table ' || rtrim(tabschema) || '.' || rtrim(tabname) || ' to user JOHN_DOE' from syscat.tables where tabschema like 'FOO%' and (type = 'T' or type = 'V')" | db2 +p -tv
This command line will generate the grants for user JOHN_DOE for all tables (T) and views (V) of any schema starting with FOO.
If you have many tables, the output will be very big and the internal buffer will be filled. Reissue the command by generating the grants for a smaller set of tables.
If you are not sure about what you are going to execute, issue the previous command without the final part (| db2 +p -tv), this will write the commands in the standard output. However, this part is the most important, because this executes the generated output.
For more details, please check the InfoCenter or my blog http://angocadb2.blogspot.com/2011/12/ejecutar-la-salida-de-un-query-en-clp.html (In Spanish)
How do I drop all tables whose name start with, say, doors_? Can I do some sort of regex using the drop table command?
I prefer not writing a custom script but all solutions are welcomed. Thanks!
This script will generate the DDL commands to drop them all:
SELECT 'DROP TABLE ' || t.oid::regclass || ';'
FROM pg_class t
-- JOIN pg_namespace n ON n.oid = t.relnamespace -- to select by schema
WHERE t.relkind = 'r'
AND t.relname ~~ E'doors\_%' -- enter search term for table here
-- AND n.nspname ~~ '%myschema%' -- optionally select by schema(s), too
ORDER BY 1;
The cast t.oid::regclass makes the syntax work for mixed case identifiers, reserved words or special characters in table names, too. It also prevents SQL injection and prepends the schema name where necessary. More about object identifier types in the manual.
About the schema search path.
You could automate the dropping, too, but it's unwise not to check what you actually delete before you do.
You could append CASCADE to every statement to DROP depending objects (views and referencing foreign keys). But, again, that's unwise unless you know very well what you are doing. Foreign key constraints are no big loss, but this will also drop all dependent views entirely. Without CASCADE you get error messages informing you which objects prevent you from dropping the table. And you can then deal with it.
I normally use one query to generate the DDL commands for me based on some of the metadata tables and then run those commands manually. For example:
SELECT 'DROP TABLE ' || tablename || ';' FROM pg_tables
WHERE tablename LIKE 'prefix%' AND schemaname = 'public';
This will return a bunch of DROP TABLE xxx; queries, which I simply copy&paste to the console. While you could add some code to execute them automatically, I prefer to run them on my own.