PostgreSQL Database Commands Tutorial
This guide outlines common PostgreSQL commands to connect to a database, list tables, describe table structures, query data, and update data.
1. Connect to the Database
Use the following command to connect to the database:
1
psql -h postgres -U user -d db_name -p 5432
After executing the command, you’ll be prompted for the password:
1
your_secure_password
2. List Tables in the Database
Once connected, list all tables with:
1
\dt;
3. Describe the Tables
To view the structure of specific tables, use the \d command:
1
\d users;
1
\d settings;
4. Check Values in a Table
To view all rows from the table_name table, execute:
1
SELECT * FROM table_name;
5. Check Table Structure
To see the structure of the table_name table, use:
1
\d table_name
6. (Optional) Filter Rows where reminder_sent is true
To view only the rows in the subscriptions table where reminder_sent is true, run:
1
SELECT * FROM subscriptions WHERE reminder_sent = true;
7. Reset All reminder_sent Values to false
To update the reminder_sent column and set all values to false, run:
1
UPDATE subscriptions SET reminder_sent = false;
After updating, you can verify the changes with:
1
SELECT reminder_sent FROM subscriptions;
This guide provides a quick reference for some useful PostgreSQL commands. For more detailed information, refer to the official PostgreSQL documentation.