Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PostgreSQL - CAST
Next article icon

PostgreSQL – Psql commands

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.

Here, we highlight some of the most frequently used PSQL commands, detailing their functionalities to enhance your PostgreSQL experience.

Top PSQL Commands in PostgreSQL

Here are the top 22 PSQL commands that are frequently used when querying a PostgreSQL database:

Serial No. Command Description
1 psql -d database -U user -W Connects to a database under a specific user
2 psql -h host -d database -U user -W Connect to a database that resides on another host
3 psql -U user -h host "dbname=db sslmode=require" Use SSL mode for the connection
4 \c dbname Switch connection to a new database
5 \l List available databases
6 \dt List available tables
7 \d table_name Describe a table such as a column, type, modifiers of columns, etc.
8 \dn List all schemes of the currently connected database
9 \df List available functions in the current database
10 \dv List available views in the current database
11 \du List all users and their assign roles
12 SELECT version(); Retrieve the current version of PostgreSQL server
13 \g Execute the last command again
14 \s Display command history
15 \s filename Save the command history to a file
16 \i filename Execute psql commands from a file
17 \? Know all available psql commands
18 \h Get help
19 \e Edit command in your own editor
20 \a Switch from aligned to non-aligned column output
21 \H Switch the output to HTML format
22 \q Exit psql shell

Additional Information:

  • The -d option in psql commands is used to state the database name.
  • The -U option specifies the database user.
  • The -h option indicates the host on which the database server resides.
  • The \h ALTER TABLE can be used to get detailed information on the ALTER TABLE statement.

Conclusion 

Understanding and utilizing Psql commands can significantly enhance your efficiency when working with PostgreSQL. Here we provided an overview of essential commands to help you manage your databases more effectively. These commands are vital tools for any PostgreSQL user.

The psql interface is powerful and allows you to do many things, including running SQL statements. If you want to get started with PostgreSQL or deepen your knowledge, check out our PostgreSQL Tutorial. You can also check out the Postgre articles on our blog.



Next Article
PostgreSQL - CAST

R

RajuKumar19
Improve
Article Tags :
  • PostgreSQL
  • postgreSQL-basics

Similar Reads

  • Rust and PostgreSQL
    In today's world of software development, choosing the right programming language and database can significantly impact the performance and reliability of your applications. Rust is a modern programming language that prioritizes safety and performance, while PostgreSQL is a powerful and flexible rel
    8 min read
  • PostgreSQL - COMMIT
    The COMMIT command in PostgreSQL is important for saving the changes made during a transaction. Without executing a COMMIT, all the data manipulation operations performed within the transaction will be lost once the session ends. It ensures that the changes made to the database are permanent and vis
    4 min read
  • PostgreSQL - CAST
    The PostgreSQL CAST function provides an efficient way to convert data types in PostgreSQL, which is important when ensuring data is in the correct format for storage, calculations, or comparisons. In PostgreSQL, we can use CAST to transform data between various data types, such as converting string
    3 min read
  • PostgreSQL - Joins
    The PostgreSQL JOIN statement is a powerful tool for combining data or rows from one or more tables based on a common field between them. These common fields are typically the primary key in the first table and the foreign key in the other table(s). By using different types of JOINs, we can perform
    5 min read
  • PostgreSQL Clients
    The PostgreSQL client is a command-line tool used to interact with PostgreSQL databases. It allows users to manage databases, execute SQL queries, and perform various administrative tasks without needing a graphical interface. In this article we will cover the key features of the PostgreSQL client,
    4 min read
  • PostgreSQL String Functions
    PostgreSQL is a powerful, open-source relational database management system that offers a rich set of functions and operators for working with string data. String manipulation is an essential task in many applications, and PostgreSQL provides a variety of built-in functions to make working with text
    8 min read
  • PostgreSQL - REPLACE() Function
    PostgreSQL REPLACE() function is a powerful tool for efficient string manipulation within our database. By utilizing the REPLACE() syntax in PostgreSQL, we can easily replace specific substrings within a string and enabling us to clean, format and modify data effectively. In this article, We will le
    4 min read
  • PostgreSQL - ADD COLUMN
    In PostgreSQL, the ADD COLUMN statement is a powerful command used to modify an existing database table by adding one or more new columns. This feature is important for adapting table structures to meet evolving data requirements, and it plays a key role in database management and optimization. In t
    5 min read
  • PostgreSQL - RENAME COLUMN
    Renaming columns in PostgreSQL is a common task for developers and database administrators. When aligning with naming conventions, fixing typos, or restructuring database schemas. Using the PostgreSQL ALTER TABLE RENAME COLUMN statement, we can efficiently rename one or more columns without losing d
    5 min read
  • PostgreSQL - GRANT
    In PostgreSQL, the GRANT statement is a powerful tool used to assign privileges to a role, allowing it to alter database objects like tables, views, functions, and more. Here we will learn about the syntax and application of the GRANT statement, with examples to illustrate its usage in PostgreSQL. S
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences