Access the psql
Shell:
Open your command prompt or terminal and enter the psql
command. You might need to provide the necessary credentials, like username and password, to log in. For example:
Copy code
psql -U your_username -d your_database
List of Vital psql
Commands:
Here are some commonly used psql
commands that are vital for working with PostgreSQL from the command line:
\\l
: List all databases.\\c database_name
: Connect to a specific database.\\dt
: List all tables in the current database.\\du
: List all roles (users) in the current database.\\q
: Quit the psql
shell.\\?
: List available psql
commands.\\h SQL_COMMAND
: Get help for a specific SQL command. For example, \\h SELECT
provides information about the SELECT
statement.SELECT: Used to retrieve data from one or more tables.
sqlCopy code
SELECT column1, column2 FROM table_name WHERE condition;
INSERT: Used to add new records (rows) into a table.
sqlCopy code
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE: Used to modify existing records in a table.
sqlCopy code
UPDATE table_name SET column1 = new_value WHERE condition;
DELETE: Used to remove records from a table.
sqlCopy code
DELETE FROM table_name WHERE condition;
CREATE TABLE: Used to create a new table with specified columns and data types.
sqlCopy code
CREATE TABLE table_name (
column1 data_type,
column2 data_type,
...
);
ALTER TABLE: Used to modify an existing table structure (e.g., add, modify, or delete columns).
sqlCopy code
ALTER TABLE table_name
ADD column_name data_type;
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type;
ALTER TABLE table_name
DROP COLUMN column_name;