link

Psql

Psql commands for managing databases, tables, and queries

19 commands

Commands

19 commands available
psql

Connect to PostgreSQL

Connect to a PostgreSQL database using psql

psql -U username -d dbname
connection
database
psql

List Databases

Show all PostgreSQL databases

l
database
info
psql

List Tables

Show all tables in the current database

dt
table
info
psql

Execute SQL Query

Run a SQL query from the terminal

psql -c "SELECT * FROM tablename;"
query
execution
psql

Backup Database

Create a backup of a PostgreSQL database

pg_dump -U username dbname > backup.sql
backup
psql

Restore Database

Restore a PostgreSQL database from a backup file

psql -U username dbname < backup.sql
restore
psql

Create Database

Create a new PostgreSQL database

createdb -U username dbname
database
create
psql

Drop Database

Delete a PostgreSQL database

dropdb -U username dbname
database
delete
psql

Create User

Create a new PostgreSQL user

createuser -U username username
user
create
psql

Drop User

Delete a PostgreSQL user

dropuser -U username username
user
delete
psql

Create Table

Create a new PostgreSQL table

CREATE TABLE tablename (column1 datatype, column2 datatype, ...);
table
create
psql

Drop Table

Delete a PostgreSQL table

DROP TABLE tablename;
table
delete
psql

Create Index

Create a new PostgreSQL index

CREATE INDEX indexname ON tablename (columnname);
index
create
psql

Drop Index

Delete a PostgreSQL index

DROP INDEX indexname;
index
delete
psql

Create View

Create a new PostgreSQL view

CREATE VIEW viewname AS SELECT column1, column2, ... FROM tablename;
view
create
psql

Drop View

Delete a PostgreSQL view

DROP VIEW viewname;
view
delete
psql

Create Function

Create a new PostgreSQL function

CREATE FUNCTION functionname(arguments) RETURNS return_type AS $$ BEGIN ... END; $$ LANGUAGE plpgsql;
function
create
psql

Drop Function

Delete a PostgreSQL function

DROP FUNCTION functionname;
function
delete
psql

Create Trigger

Create a new PostgreSQL trigger

CREATE TRIGGER trigger_name AFTER INSERT ON tablename FOR EACH ROW EXECUTE FUNCTION function_name();
trigger
create