EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Postgres List Schemas

By Payal UdhaniPayal Udhani

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » Postgres List Schemas

Postgres List Schemas

Introduction to Postgres List Schemas

The database administrator and manager need to be aware of all the database server’s environment. In the case of PostgreSQL, the server can handle data from multiple databases at a single time. The database administrator needs to know about all the databases and the tables and schema related data about the database server. This article will learn how we can check and view the schema related information in PostgreSQL.

We will begin by knowing what schema is in the database. In any database, all the information about the objects being managed in it is stored in the collection of the views called schema. The information schema remains stable and is portable as it is defined inside the SQL standards. However, the system catalogs are variable as they contain specific information and are modelled after PostgreSQL implementation. The schema is accessible to the first user of the database and has all the privileges to handle and operate on the schema, including dropping it.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Datatypes – Postgres List Schemas

The views of information schema use a special type of data types that are simple domains generated over the built-in ones. These data types are not to be used in our outer work related to our databases. It is exclusively used by information schema view only. However, when the information is fetched from the information schema, you need to handle them in your application. All columns in information schema views can belong to one of the following datatypes.

Data type Description
cardinal_number It is a non-negative, i.e. positive integer
character_data It specifies the string of characters that does not has any maximum length specified, which is used for fields containing data other than SQL identifiers.
sql_identifier It specifies the string of characters that does not have any maximum length specified, used for SQL identifiers.
time_stamp It is the domain defined over timestamp, which also considers time zones.
yes_or_no It is the string of characters that can either have YES or NO value in it. It is kept for backward compatibility of information schema as information schema was created and came into usage even before the boolean data type was added in the SQL standards, which had either true/false value.

MetaCommands

Instead of using raw SQL queries for listing the data from the database, we can use metacommands that are short and precise and can be used with psql. Psql evaluates these metacommands and even translates them to SQL raw commands sometimes provided if they issued in the server’s system tables. A backslash recognizes metacommands and the command keyword followed by the parameters if any, if you want to pass to the query.

Most of the PostgreSQL servers have three default databases when they are created, which are namely template0, template1, and Postgres. Template0 and template1 are the basic databases that are internally used by the command CREATE DATABASE for its usage. These two databases are called skeleton databases. The default database which is SELECTed and shown to you is the Postgres database. After that, you can create databases of your choice and switch to them to create and manipulate tables in your databases. All databases can be retrieved and listed using the metacommand \list or \l and can be switched from one to other using \connect or \c. We can list out all the tables using the metacommand \dt command.

How to Use Postgres List Schemas?

Now, we will see how we can list databases using the psql command.\list, or \l can be used.

Open your PostgreSQL command prompt and then type SQL to get its command prompt. No type \list and press enter. These are the output that you will see “Three default databases of PostgreSQL”.

Code:

sudo su - postgres
psql

Output:

psql command

Let us create one new database named demo by using the createdb command. Exit the psql by typing \q and then just type createdb demo and press enter. Then again, enter \list and press enter to list out all the databases after typing psql to get the command prompt of psql. The output will be as follows –

Code:

\q
createdb demo
psql
\list

Output:

psql command

Schemas

There are three types of schemas present in any PostgreSQL database: information schemas, temporary schemas, and default pg_* schemas. Other than the user-defined and public schemas, one more type of schema present in PostgreSQL is the pg_catalog schema. This schema contains the information about and the system tables, operators, data types and functions present in the current database. We will create a query to retrieve from the pg_catalog schema to list out all the user-related schemas.

Code:

SELECT * FROM pg_catalog.pg_namespace;

Output:

Postgres List Schemas - 3

The field nspname displays the names of the schemas. These are all the schemas present in our database right now. The npsowner field stores the user id which owns that schema. We can see that the user with id 10 owns all schemas. To get the information about the user with id 10, we can query on the pg_user table.

Code:

SELECT * FROM pg_catalog.pg_user where usesysid = 10;

Output:

Postgres List Schemas - 4

We can say that the user with usesysid =10 is postgres which owns all the above-retrieved schemas. If we want to retrieve the name os the schema and the person owning it in a single output, we can create a join on pg_namespace and pg_user as shown below.

Code:

SELECT s.nspname AS table_schema, u.usename AS OWNER FROM pg_catalog.pg_namespace s JOIN pg_catalog.pg_user u ON u.usesysid = s.nspowner ORDER BY table_schema;

Output: 

Postgres List Schemas - 5

Conclusion

PostgreSQL databases provide us with metacommands that are compact and immensely useful for database administrators and managers to check the database environments and structure related information faster and effectively during their daily routines. Schemas can be retrieved from the system tables of pg_catalog schema, which is present in PostgreSQL databases. pg_namespace and pg_user are two main tables that convey schema related information to us.

Recommended Articles

This is a guide to Postgres List Schemas. Here we discuss an introduction to Postgres List Schemas, with appropriate syntax, command and examples. You can also go through our other related articles to learn more –

Popular Course in this category
Sale
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,206 ratings)
Course Price

View Course

Related Courses
  1. PostgreSQL GRANT
  2. PostgreSQL CASE Statement
  3. PostgreSQL For Loop
  4. PostgreSQL While Loop

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More