Atikh's DBA blog
  • Home
  • Oracle
  • MySQL
  • MongoDB
  • PostgreSQL
  • Snowflake
  • About Me
  • Contact Us

List Databases, Tables, schemas and other basic commands in PostgreSQL

 Atikh Shaikh     PostgreSQL     No comments   

In previous articles we have learned about basics of PostgreSQL, architecture and installation of PostgreSQL on windows
(Read here Introduction to PostgreSQL , Installation of PostgreSQL )
Now we will learn basic commands to administrates the PostgreSQL databases
list databases, tables,schemas and other basic queries in postgresql, postgresql blog

Login to PostgreSQL
we can login to postgres database using psql with few options like 
database name and postgres user, below we logged in to tech_pg database using user postgreu
$ psql -d tech_pg -U postgreu
psql.bin (9.6.2.7)
Type "help" for help.

tech_pg=#
 

Listing the database available
A single PostgreSQL server instance can have multiple databases, each and every database is stored as separate file under its own directory, to list database we can use \l or we can use pg_database table
tech_pg=# \l
                                             List of databases
     Name     |   Owner    | Encoding |   Collate   |    Ctype    | ICU |        Access privileges
--------------+------------+----------+-------------+-------------+-----+----------------------------------
 tech_pg      | postgreu | UTF8     | en_US.UTF-8 | en_US.UTF-8 |     |
 postgres     | postgreu | UTF8     | en_US.UTF-8 | en_US.UTF-8 |     |
template0     | postgreu | UTF8     | en_US.UTF-8 | en_US.UTF-8 |     | =c/postgreu                   +
              |            |          |             |             |     | postgreu=CTc/postgreu
 template1    | postgreu | UTF8     | en_US.UTF-8 | en_US.UTF-8 |     | =c/ postgreu                   +
              |            |          |             |             |     | postgreu=CTc/postgreu
(4 rows)

tech_pg=#
tech_pg=# select datname from pg_database;
   datname
--------------
 postgres
 tech_pg
 template1
 template0
 (4 rows)

tech_pg=#


Connect to database (Switch Database)
By default PostgreSQL server instance has 3 databases i.e. postgres, template1 and template0
one can connect to database from postgres command line tool and switch to other database using \connect or \c

tech_pg=# \c postgres
You are now connected to database "postgres" as user "postgreu".
postgres=#
postgres=# \connect tech_pg
You are now connected to database "tech_pg" as user "postgreu".
tech_pg=#

Checking Current Database

We can check the name of the database in which we are currently logged in using different methods, i.e. using \c, \conninfo, select * from current_catalog and current_database() function. it has been illustrated below 

tech_pg-# \c tech_pg
You are now connected to database "tech_pg" as user "postgreu".
tech_pg-#
tech_pg=# \conninfo
You are connected to database "tech_pg" as user "postgreu" via socket in "/tmp" at port "5444".
tech_pg=#
tech_pg=# \c
You are now connected to database "tech_pg" as user "postgreu".
tech_pg=#
tech_pg=# select * from current_catalog;
 current_database
------------------
 tech_pg
(1 row)

tech_pg=# select current_database() ;
 current_database
------------------
 tech_pg
(1 row)

tech_pg=#

Listing the Tables
one can simply list the tables created under specific database using \dt command, in Below example we can see there is no table present in database, if any present then it will list out with details.


tech_pg=# select current_database() ;
 current_database
------------------
 tech_pg
(1 row)

tech_pg=# \dt
No relations found.
tech_pg=#

Get list of all Schema
using command \dn, one can simply list out all the schema in postgres

tech_pg=# \dn
         List of schemas
        Name        |   Owner
--------------------+------------
 postgreu           | postgreu
 dbms_job_procedure | postgreu
 pgagent            | postgreu
 public             | postgreu
(4 rows)

One can use information_schema.schemata view to list out all the schemas in current database 

tech_pg=# select catalog_name,schema_name,schema_owner from information_schema.schemata;
 catalog_name| schema_name    | schema_owner
-------------+--------------------+--------------
 tech_pg | pg_toast           | postgreu
 tech_pg | pg_temp_1          | postgreu
 tech_pg | pg_toast_temp_1    | postgreu
 tech_pg | pg_catalog         | postgreu
 tech_pg | public             | postgreu
 tech_pg | information_schema | postgreu
 tech_pg | sys                | postgreu
 tech_pg | dbo                | postgreu
(8 rows)

Comment for feedback or in case need additional information, share the post if you like it.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Author

Atikh Shaikh
View my complete profile

Categories

  • MongoDB (18)
  • Oracle 12c (30)
  • Oracle12cR2 New Feature (3)
  • PostgreSQL (20)
  • RMAN (10)
  • Snowflake (8)
  • mysql (23)
  • oracle (74)

Blog Archive

  • ▼  2018 (38)
    • ►  November (25)
    • ▼  December (13)
      • Starting and Stopping MongoDB
      • Oracle Database Architecture Physical and Logical
      • MongoDB Database Backup and Restore
      • Startup and Shutdown Oracle Database
      • Oracle 12c New features – Multitenant Database
      • MongoDB Storage Engines
      • Create Pluggable Database (PDB) in Oracle 12c
      • Warning: PDB altered with errors- opening PDB in O...
      • Oracle 12c: Starting and Stopping PDB
      • Know your Hostname in MySQL
      • Everything you need to know about Oracle Data Pump
      • List Databases, Tables, schemas and other basic c...
      • User Managed Backups in Oracle
  • ►  2019 (33)
    • ►  January (15)
    • ►  February (6)
    • ►  March (2)
    • ►  April (5)
    • ►  May (5)
  • ►  2020 (5)
    • ►  April (1)
    • ►  May (2)
    • ►  July (2)
  • ►  2021 (8)
    • ►  June (3)
    • ►  July (3)
    • ►  August (1)
    • ►  December (1)
  • ►  2022 (33)
    • ►  May (3)
    • ►  June (10)
    • ►  July (3)
    • ►  August (4)
    • ►  September (8)
    • ►  October (3)
    • ►  November (2)
  • ►  2023 (14)
    • ►  February (1)
    • ►  April (5)
    • ►  May (2)
    • ►  June (1)
    • ►  September (1)
    • ►  October (1)
    • ►  December (3)
  • ►  2024 (5)
    • ►  January (2)
    • ►  March (3)
  • ►  2025 (6)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)

Popular Posts

  • ORA-29283: invalid file operation: unexpected "LFI" error (1509)[29437]
    I was trying to export the schema in my windows PC, it got stuck with below error    C:\Users\shaik\Videos\technodba exp>expdp userid...
  • PostgreSQL : How to get data directory location for PostgreSQL instance
    Sometimes, you start working on a PostgreSQL instance but forget about the data directory, here we will discuss different methods to know th...
  • Oracle 23ai : Use of NOVALIDATE Constraints in IMPDP
    While performing impdp operations in the Oracle database, Oracle performs validation checks for every constraint on the imported table, that...
  • ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
    In previous articles, we have learned about user creation and grants  in MySQL in detail, but there are a few privileges called global priv...
  • Oracle Dataguard Broker Configuration (DGMGRL)
    Data Guard Broker is a command-line interface that makes managing primary and standby databases easy. DBA can use a single command to switch...

Labels

oracle Oracle 12c mysql PostgreSQL MongoDB oracle 19c Oracle23c oracle19c Orale PDB-CDB oracle12c python AWS Oracle ASM Virtualbox pluggable database storage engine

Pages

  • Disclaimer
  • Privacy Policy

Follow TechnoDBA

Copyright © Atikh's DBA blog | Powered by Blogger