{"id":20962913,"url":"https://github.com/alvachon/postgres_note","last_synced_at":"2026-04-29T16:39:10.953Z","repository":{"id":166434370,"uuid":"571311945","full_name":"alvachon/postgres_note","owner":"alvachon","description":"Notes to use postgreSQL on Linux, mostly commands for the command line.","archived":false,"fork":false,"pushed_at":"2022-12-07T02:27:06.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T07:24:50.438Z","etag":null,"topics":["database","postgresql","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alvachon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-11-27T20:26:37.000Z","updated_at":"2022-12-06T04:45:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"87d0cea4-8974-4461-a8c3-4f0b0e09cdc4","html_url":"https://github.com/alvachon/postgres_note","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alvachon/postgres_note","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvachon%2Fpostgres_note","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvachon%2Fpostgres_note/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvachon%2Fpostgres_note/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvachon%2Fpostgres_note/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alvachon","download_url":"https://codeload.github.com/alvachon/postgres_note/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvachon%2Fpostgres_note/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269857911,"owners_count":24486438,"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","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["database","postgresql","sql"],"created_at":"2024-11-19T02:39:05.006Z","updated_at":"2026-04-29T16:39:10.904Z","avatar_url":"https://github.com/alvachon.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL Notes\n\n## Introduction\nPostgreSQL is the most popular open source relational database\n \n # #  Installation\n### Install on Ubuntu\n```\nsudo apt update\nsudo apt upgrade\nsudo apt install postgresql postgresql-contrib\nsudo apt-get install postgresql-(version)\n```\n\n### Install pgAdmin 4 on Ubuntu (GUI)\n[Official Page](https://www.pgadmin.org/download/pgadmin-4-apt/)\n\n### General information\n```\nman psql\npsql --help \n``` \n\n### Calling the program\n```\nsudo -i -u postgres\npsql\n```\n### View command\nList of databases\n```\n\\l\n```\n\nList of roles\n```\n\\du\n```\n\n### Modify or Create  or Delete role\n```\nALTER USER (role) WITH PASSWORD '(password)';\nCREATE USER (new role) WITH PASSWORD '(password)';\nALTER USER (role) WITH SUPERUSER;\nDROP USER (role);\n```\n\n### How to link pgAdmin 4 with postgres\n*Open pgAdmin4, open a new server registration.* \\\n**Name :** Localhost \\\n**Host :** 127.0.0.1 (Local Host) \\\n**Username :** (role with superuser in postgres) \\\n**Password :** (the one of the superuser)\n*Go to terminal* \\\n```\npsql -h localhost -p 5432 -U (user) (database name)\n```\n\n## Creating or deleting a database\n```\n CREATE DATABASE name_database; \n DROP DATABASE name_database;\n ```\n ## Moving into the database\n ```\n \\c database_name\n ```\n  ## Show all table into the database\n ```\n \\dt\n ```\n \n ## How to create a table\n ```\n \\c database_name\n CREATE TABLE table_name ( #enter\n Column_name data_type constraint_or_not,\n id BIGSERIAL NOT NULL PRIMARY KEY,\n first_name VARCHAR(50) NOT NULL,\n last_name VARCHAR(50) NOT NULL,\n date_of_birth DATE NOT NULL,\n email VARCHAR(150));\n```\n## How to insert record into tables\n```\nINSERT INTO table_name (first_name, last_nam e, gender, date_of_birth)\nVALUES ('Anne', 'Smith', 'FEMALE', date '1988-01-09');\n```\n## How to import from a file\n ```  \n \\i /Users/../../blabla.sql\n SELECT * FROM table_name;\n ```\n ## How to sort by order\n ```\n SELECT * FROM table_name ORDER BY table_section;\n  ```\n ## How to sort distinct\n ```\nSELECT DISTINCT table_selection FROM table_name ORDER BY table_selection; \n```\n## How to sort by some parts\n```\nSELECT * FROM person WHERE gender = 'Female';\n``` \n## How to sort with AND or OR or IN conditions\n```\nSELECT * FROM person WHERE gender = 'Male' AND (country_of_birth = 'Poland' OR country_of_birth = 'China');\nSELECT *\nFROM person\nWHERE country_of_birth IN ('China', 'Brazil', 'France')\nORDER BY country_of_birth;  \n```\n## How to sort with limit\n```\n SELECT * FROM person LIMIT 10;\n //To target the 5 next one\n SELECT * FROM person OFFSET 5 LIMIT 5;\n //SQL standard way : \n SELECT * FROM person OFFSET 5 FETCH FIRST 5 ROW ONLY;\n ```\n ## How to select data from a range\n ```\n SELECT * FROM person\n WHERE date_of_birth\n BETWEEN DATE '2000-01-01' AND '2015-01-01';\n ``` \n ## How to select similar element\n  ``` \n  SELECT * FROM person WHERE email LIKE '%.com';\n  SELECT * FROM person WHERE email LIKE '_____';\n  ## Without uppercase sensitivites\n  SELECT * FROM person WHERE country_of_birth ILIKE 'p%';\n  ```\n  ## How to count items in each element table\n  ```\n  SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth;\n  SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth ORDER BY country_of_birth;\n  ```\n  ## How to show count of each, with more than 5\n  ```\n  SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth HAVING COUNT(*) \u003e 5 ORDER BY country_of_birth;\n  ```\n  \n ## Comparison operator\n ```\n SELECT 1 = 1;\n ```\n return t for true\n ```\n SELECT 1 \u003c 1;\n ```\n return f for false\n \n ## Function aggregation\n * count(*)\n * max\n * min\n \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvachon%2Fpostgres_note","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falvachon%2Fpostgres_note","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvachon%2Fpostgres_note/lists"}