PostgreSQL Made Easy: Essential CLI Commands and Pro Tips for Developers

June 202514 min readAbhoy Sarkar

PostgreSQL, or Postgres, is one of the most powerful, reliable, and feature-rich relational databases available today. Whether you're building small projects or enterprise-level applications, understanding Postgres at both the CLI and the code level can make your life as a developer much easier.

In this article, we’ll explore not only how to use PostgreSQL effectively but also dive into some essential CLI commands (\d, \c, \l, \x) that every developer should know. Plus, we’ll touch on how asynchronous processes in JavaScript interact with databases in the browser context, providing a full-stack perspective.

Why PostgreSQL is a Developer Favorite

Postgres stands out for several reasons: it’s open-source, ACID-compliant, and extremely versatile. It supports advanced features like JSONB storage, full-text search, and procedural functions while maintaining high reliability.

  • Strong SQL compliance with rich indexing options.
  • Support for advanced data types and extensions (PostGIS, pgcrypto).
  • Active community and extensive documentation.
  • Scalable for both small projects and enterprise-grade applications.

Getting Started with the Postgres CLI

Postgres comes with a command-line client called psql. Knowing a few key commands can drastically improve your workflow, whether you’re inspecting database schemas, switching between databases, or running quick queries.

1. List All Databases: \l

The \l command lists all databases in your Postgres server. It’s useful when you’re connecting to a new environment or checking what databases exist on your instance.

sql
postgres=# \l
                           List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 mydb      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

2. Connect to a Database: \c

To switch databases within psql, use \c <database_name>. This is faster than exiting and reconnecting, and allows you to run queries in different databases without leaving the CLI.

sql
postgres=# \c mydb
You are now connected to database "mydb" as user "postgres".

3. View Table Structures: \d

The \d command displays the schema of a table, including columns, types, and constraints. This is essential for understanding the database structure, especially in large applications or when joining unfamiliar tables.

sql
mydb=# \d users
         Table "public.users"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 id     | serial  |           | not null | 
 name   | text    |           |          | 
 email  | text    |           |          | 
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

4. Expand Table Details for Readability: \x

The \x command toggles extended display in psql. When enabled, query results are displayed vertically, making wide tables or long outputs easier to read.

sql
mydb=# \x
Expanded display is on.
mydb=# SELECT * FROM users WHERE id = 1;
-[ RECORD 1 ]-------------------------
id    | 1
name  | John Doe
email | john@example.com

Best Practices for Using Postgres Effectively

Using Postgres efficiently isn’t just about running queries. It involves schema design, indexing, and careful handling of asynchronous operations in your applications.

  • Design tables with meaningful constraints and indexes to speed up queries.
  • Use transactions for multiple related operations to maintain consistency.
  • Analyze query performance using EXPLAIN and EXPLAIN ANALYZE.
  • Leverage JSONB when storing flexible, semi-structured data.
  • Regularly vacuum and monitor your database for bloat and performance issues.

JavaScript and Asynchronous Database Operations

When your Node.js backend communicates with Postgres, queries are asynchronous, meaning the server can handle other requests while waiting for the database response. Node.js uses non-blocking I/O under the hood, allowing your backend to efficiently process multiple database operations without freezing the server.

For example, using async/await or Promises with a Postgres client library like pg ensures that your application remains responsive, even when performing long-running queries.

js
const { Client } = require('pg');

async function getUsers() {
  const client = new Client();
  await client.connect();
  const res = await client.query('SELECT * FROM users');
  console.log(res.rows);
  await client.end();
}

getUsers();
console.log('Query sent, waiting for results...');

In this example, the console will immediately print 'Query sent, waiting for results...' while the database query runs asynchronously.

Tips for Smooth Workflow with Postgres CLI

  • Combine \d with \x for more readable table structures.
  • Use \c to switch databases without quitting psql.
  • List all databases with \l to quickly verify environments.
  • Create scripts for common queries to save time.
  • Use aliases or shell scripts to automate frequent database connections.

Conclusion

PostgreSQL is a powerful tool for developers, but mastering it requires more than just writing queries. Using the CLI effectively with commands like \d, \c, \l, and \x gives you insight into your database structure and workflow, while following best practices ensures performance and maintainability.

Start small, experiment with CLI commands, monitor your queries, and watch how much more control you gain over your Postgres environment.

Tags

postgresqldatabaseclisqlbackendproductivity