0% found this document useful (0 votes)
61 views3 pages

PSQL Cheatsheet

This document is a PostgreSQL Cheat Sheet covering both CLI (`psql`) and GUI tools for essential operations. It includes commands for logging in, useful meta commands, basic SQL operations, and tips for power users. Additionally, it provides guidance for connecting and performing common operations in GUI tools like pgAdmin and TablePlus.

Uploaded by

tamizhazhagansk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

PSQL Cheatsheet

This document is a PostgreSQL Cheat Sheet covering both CLI (`psql`) and GUI tools for essential operations. It includes commands for logging in, useful meta commands, basic SQL operations, and tips for power users. Additionally, it provides guidance for connecting and performing common operations in GUI tools like pgAdmin and TablePlus.

Uploaded by

tamizhazhagansk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Here's a **PostgreSQL Cheat Sheet** for both **CLI (`psql`)** and **Query Editor

(GUI tools like pgAdmin, TablePlus, or DBeaver)** use — from login to performing
essential operations.

---

## PART 1: PostgreSQL **CLI Cheat Sheet** (`psql`)

### ✅ 1. Login to PostgreSQL from Terminal

```bash
psql -U postgres
```

> `-U postgres` is the username. You’ll be prompted for the password.

If using a specific database:

```bash
psql -U postgres -d your_database_name
```

If your database is on another host/port:

```bash
psql -h localhost -p 5432 -U postgres -d your_database_name
```

---

### ✅ 2. Useful Meta Commands in `psql`

| Command | Description |
| -------------- | ----------------------------- |
| `\l` | List all databases |
| `\c dbname` | Connect to database |
| `\dt` | List all tables in current DB |
| `\d tablename` | Describe table structure |
| `\du` | List roles/users |
| `\q` | Quit `psql` |

---

### ✅ 3. Basic SQL Operations (inside `psql`)

```sql
-- Create a database
CREATE DATABASE walkman_db;

-- Switch database
\c walkman_db

-- Create a table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert data
INSERT INTO users (name, email) VALUES ('Alice', '[email protected]');

-- Select data
SELECT * FROM users;

-- Update data
UPDATE users SET name = 'Alice Smith' WHERE id = 1;

-- Delete data
DELETE FROM users WHERE id = 1;

-- Drop table
DROP TABLE users;
```

---

## 🧩 PART 2: PostgreSQL **Query Editor (GUI Tools)** Cheat Sheet

### 📥 Connecting in GUI (pgAdmin, TablePlus, DBeaver)

* **Host**: `localhost`
* **Port**: `5432` (default)
* **User**: `postgres`
* **Password**: your password
* **Database**: `walkman_db` (or create one)

---

### 📋 Common Operations in GUI

| Operation | SQL Equivalent |


| ---------------- | ------------------------------------------- |
| Create Table | `CREATE TABLE ...` |
| Insert Record | `INSERT INTO ...` |
| Browse Data | `SELECT * FROM ...` |
| Edit Rows Inline | GUI-specific |
| Run SQL Script | Paste SQL into Query Editor and click "Run" |
| Export Data | Right-click table > Export/Backup |
| Import Data | Right-click table > Import/Restore |

---

### 🧪 Sample SQL (Copy/Paste into Query Editor)

```sql
-- Create sample table
CREATE TABLE sample_db (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
dob DATE
);

-- Insert dummy data


INSERT INTO sample_db (name, dob) VALUES
('James', '1990-05-12'),
('Alice', '1985-08-25'),
('Bob', '1993-03-30');

-- Query data
SELECT * FROM sample_db;
```

---

## 🛠 Tips for CLI Power Users

| Feature | Command
|
| ------------------------- |
------------------------------------------------------------ |
| Run SQL from file | `\i path/to/file.sql`
|
| Export to CSV | `\COPY (SELECT * FROM sample_db) TO 'output.csv' CSV
HEADER` |
| Pretty print | `\x` (toggle expanded output)
|
| Show query execution time | `\timing`
|

---

## ✅ To Exit `psql`:

```bash
\q
```

---

If you want, I can also generate **SQL files** or **Prisma schema mappings** based
on your schema needs.

You might also like