{"id":19128749,"url":"https://github.com/ahmedosamamath/postgresql-basics","last_synced_at":"2026-03-11T12:13:12.495Z","repository":{"id":260998104,"uuid":"859778267","full_name":"AhmedOsamaMath/postgresql-basics","owner":"AhmedOsamaMath","description":"A beginner-friendly guide to PostgreSQL covering installation, basic commands, SQL queries, and database management techniques.","archived":false,"fork":false,"pushed_at":"2024-09-20T03:02:33.000Z","size":45,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-06T00:06:07.106Z","etag":null,"topics":["postgres","postgresql","postgresql-database","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AhmedOsamaMath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-19T09:06:07.000Z","updated_at":"2025-04-06T14:46:32.000Z","dependencies_parsed_at":"2024-11-04T05:30:38.194Z","dependency_job_id":"c7e6055a-e1d2-487d-9aa1-44ad735e5271","html_url":"https://github.com/AhmedOsamaMath/postgresql-basics","commit_stats":null,"previous_names":["ahmedosamamath/postgresql-basics"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedOsamaMath%2Fpostgresql-basics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedOsamaMath%2Fpostgresql-basics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedOsamaMath%2Fpostgresql-basics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedOsamaMath%2Fpostgresql-basics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AhmedOsamaMath","download_url":"https://codeload.github.com/AhmedOsamaMath/postgresql-basics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252596399,"owners_count":21773844,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["postgres","postgresql","postgresql-database","sql"],"created_at":"2024-11-09T06:05:27.664Z","updated_at":"2025-10-03T17:14:48.511Z","avatar_url":"https://github.com/AhmedOsamaMath.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL Basics\n\nThis repository contains an overview of basic PostgreSQL commands, installation steps, and common SQL queries. PostgreSQL is a powerful, open-source object-relational database system with a strong reputation for reliability and performance.\n\n## Table of Contents\n\n1. [Download and Install PostgreSQL](#download-and-install-postgresql)\n2. [Add PostgreSQL to PATH](#add-postgresql-to-path-for-windows)\n3. [Create Shortcuts for pgAdmin and psql Shell](#create-shortcuts-for-pgadmin-and-psql-shell)\n4. [Basic PostgreSQL Commands](#basic-postgresql-commands)\n5. [User and Role Management](#user-and-role-management)\n6. [Database Management](#database-management)\n7. [Data Types](#data-types)\n8. [Table Management](#table-management)\n9. [Data Manipulation](#data-manipulation)\n10. [Query Clauses](#query-clauses)\n11. [Indexes and Constraints](#indexes-and-constraints)\n12. [Transactions](#transactions)\n13. [Backup and Restore](#backup-and-restore)\n14. [Advanced Queries](#advanced-queries)\n\n## Download and Install PostgreSQL\n\n1. Visit the official [PostgreSQL Downloads](https://www.postgresql.org/download/) page.\n2. Select your operating system (Windows, macOS, or Linux).\n3. Follow the installation steps provided by the PostgreSQL installer.\n\n## Add PostgreSQL to PATH (for Windows)\n\n1. After installation, locate the PostgreSQL installation directory (usually in `C:\\Program Files\\PostgreSQL\\version\\bin`).\n2. Add this path to the system's environment variables:\n   - Open \"System Properties\" (Right-click `This PC` → `Properties` → `Advanced system settings`).\n   - Under \"System Properties\" → `Advanced` → Click `Environment Variables`.\n   - In the \"System Variables\" section, find `Path` and click `Edit`.\n   - Add the PostgreSQL `bin` folder to the path, and click `OK`.\n\n## Create Shortcuts for pgAdmin and psql Shell\n\n- **For pgAdmin (GUI):**\n  1. Locate `pgAdmin 4` in your installed programs or the PostgreSQL folder.\n  2. Right-click on the program → `Send to` → `Desktop (create shortcut)`.\n\n- **For psql Shell (CLI):**\n  1. Find the `SQL Shell (psql)` in the PostgreSQL directory or Start menu.\n  2. Right-click on it → `Send to` → `Desktop (create shortcut)`.\n\n## Basic PostgreSQL Commands\n\n- **Connect to a Database:**\n  ```bash\n  psql -U username -d dbname\n  ```\n\n  Once connected, type `help` for help:\n  ```\n  You are using psql, the command-line interface to PostgreSQL.\n  Type:  \\copyright for distribution terms\n         \\h for help with SQL commands\n         \\? for help with psql commands\n         \\g or terminate with semicolon to execute query\n         \\q to quit\n  ```\n\n- **Get help for psql commands before connecting:**\n  ```bash\n  psql --help\n  ```\n\n  This displays a list of available options and their descriptions, such as:\n  - `-U username` to specify the user.\n  - `-d dbname` to connect to a specific database.\n  - `-h host` to connect to a specific host.\n  - `-p port` to specify the port number.\n\n## User and Role Management\n- **Create User:**\n  ```sql\n  CREATE USER username WITH PASSWORD 'password';\n  ```\n- **Grant Superuser Privileges:**\n  ```sql\n  ALTER USER username WITH SUPERUSER;\n  ```\n- **Grant User Privileges:**\n  ```sql\n  GRANT ALL PRIVILEGES ON DATABASE dbname TO username;\n  ```\n- **List Users:**\n  ```sql\n  \\du\n  \\du+ -- for more details\n  ```\n- **Delete User:**\n  ```sql\n  DROP USER username;\n  ```\n\n## Database Management\n\n- **Create Database:**\n  ```sql\n  CREATE DATABASE dbname;\n  ```\n- **Switch to Another Database:**\n  ```sql\n  \\c dbname\n  ```\n- **List Databases:**\n  ```sql\n  \\l\n  ```\n- **Delete Database:**\n  ```sql\n  DROP DATABASE dbname;\n  ```\n- **Execute SQL Script from a File:**\n  ```sql\n  \\i /path/to/file.sql\n  ```\n\n## Data Types\n\n| Data Type         | Description                                          |\n|-------------------|------------------------------------------------------|\n| `INTEGER`         | Stores whole numbers                                 |\n| `SERIAL`          | Auto-incrementing integer                            |\n| `BIGINT`          | Stores large whole numbers                           |\n| `NUMERIC(p, s)`   | Stores exact numbers with precision and scale         |\n| `REAL`            | Stores floating-point numbers                        |\n| `DOUBLE PRECISION`| Stores double precision floating-point numbers       |\n| `VARCHAR(n)`      | Stores variable-length text, up to `n` characters     |\n| `TEXT`            | Stores variable-length text (unlimited)              |\n| `BOOLEAN`         | Stores `TRUE` or `FALSE`                             |\n| `DATE`            | Stores a date (year, month, day)                     |\n| `TIMESTAMP`       | Stores both date and time                            |\n| `UUID`            | Stores universally unique identifiers                |\n\n## Table Management\n\n- **Create a Table with `PRIMARY KEY` and `NOT NULL`:**\n  ```sql\n  CREATE TABLE tablename (\n      id SERIAL PRIMARY KEY,\n      column1 VARCHAR(255) NOT NULL,\n      column2 INTEGER NOT NULL,\n      column3 DATE\n  );\n  ```\n- **View Table Schema:**\n  ```sql\n  \\d tablename\n  ```\n\n## Data Manipulation\n- **Insert Data:**\n  ```sql\n  INSERT INTO tablename (column1, column2) VALUES (value1, value2);\n  ```\n- **Update Data:**\n  ```sql\n  UPDATE tablename SET column1 = value1 WHERE condition;\n  ```\n- **Delete Data:**\n  ```sql\n  DELETE FROM tablename WHERE condition;\n  ```\n- **Select Data:**\n  ```sql\n  SELECT column1, column2 FROM tablename WHERE condition;\n  ```\n- **Using AND, OR conditions in WHERE:**\n  ```sql\n  SELECT * FROM tablename WHERE condition1 AND (condition2 OR condition3);\n  ```\n\n## Query Clauses\n\n- **LIMIT Clause:**\n  ```sql\n  SELECT * FROM tablename LIMIT 10;\n  ```\n- **OFFSET Clause:**\n  ```sql\n  SELECT * FROM tablename LIMIT 10 OFFSET 5;\n  ```\n- **FETCH Clause:**\n  ```sql\n  SELECT * FROM tablename OFFSET 5 FETCH NEXT 10 ROWS ONLY;\n  ```\n- **IN Clause:**\n  ```sql\n  SELECT * FROM tablename WHERE column IN ('value1', 'value2', 'value3');\n  ```\n- **DISTINCT Clause:**\n  ```sql\n  SELECT DISTINCT column FROM tablename;\n  ```\n- **ORDER BY Clause:**\n  ```sql\n  SELECT * FROM tablename ORDER BY column ASC; -- Order results in ascending order (default)\n  SELECT * FROM tablename ORDER BY column DESC; -- Order results in descending order:\n  ```\n\n## Indexes and Constraints\n\n- **Create Index:**\n  ```sql\n  CREATE INDEX indexname ON tablename (columnname);\n  ```\n\n- **Add Foreign Key:**\n  ```sql\n  ALTER TABLE tablename ADD CONSTRAINT fk_name FOREIGN KEY (columnname) REFERENCES other_table (columnname);\n  ```\n\n## Transactions\n\n- **Begin Transaction:**\n  ```sql\n  BEGIN;\n  ```\n\n- **Commit Transaction:**\n  ```sql\n  COMMIT;\n  ```\n\n- **Rollback Transaction:**\n  ```sql\n  ROLLBACK;\n  ```\n\n## Backup and Restore\n\n- **Backup a Database:**\n  ```bash\n  pg_dump dbname \u003e backupfile.sql\n  ```\n- **Backup only the schema (pre-data including indexes, sequences, etc.):**\n  ```bash\n  pg_dump --schema-only dbname \u003e schema_backup.sql\n  ```\n- **Restore a Database:**\n  ```bash\n  psql dbname \u003c backupfile.sql\n  ```\n\n## Advanced Queries\n\n- **Join Tables:**\n  ```sql\n  SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;\n  ```\n\n- **Group By:**\n  ```sql\n  SELECT column, COUNT(*) FROM tablename GROUP BY column;\n  ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedosamamath%2Fpostgresql-basics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahmedosamamath%2Fpostgresql-basics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedosamamath%2Fpostgresql-basics/lists"}