MCPcopy Index your code
hub / github.com/dwyl/learn-postgresql

github.com/dwyl/learn-postgresql @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
20 symbols 42 edges 11 files 16 documented · 80%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Learn PostgreSQL

Learn how to use PostgreSQL and Structured Query Language (SQL) to store and query your data.

Build Status codecov.io Dependencies: None! devDependencies Status contributions welcome

Why?

Helping people store, retrieve and derive insights from data is the essence of all software applications.

SQL is Everywhere

Like it or not, Relational Databases store most of the world's structured data and Structured Query Language (SQL) is by far the most frequent way of retrieving the data.

According to the most recent surveys/statistics, SQL still dominates the world of databases.

https://insights.stackoverflow.com/survey/2018/#technology-databases stackoverflow-survey-2018-databases

https://db-engines.com/en/ranking dbms-ranking

Note: you should never adopt a technology based on it's current popularity, also be ware of "argumentum ad populum" ("it's popular therefore you should use it"). Always pick the appropriate tool for the job based on the requirements, constraints and/or availability (both of "skill" on your existing team or in the wider community). We include these stats to explain that relational databases are still the most widely used by far and so learning SQL skills is a very wise investment both as an individual and for your team or organisation.

PostgreSQL is Easy to Learn and it Runs Everywhere!

Getting started with PostgreSQL is easy, (just follow the steps in this guide and try out the example queries!)

When you are ready to deploy your app, you are in safe hands, PostgreSQL runs everywhere:

  • Travis-CI (free) Integration Testing: https://docs.travis-ci.com/user/database-setup/#postgresql
  • Heroku PostgreSQL (free for MVP: 10k rows): https://www.heroku.com/postgres
  • AWS RDS Postgres (good value + high performance): https://aws.amazon.com/rds/postgresql/
  • Google Cloud SQL: https://cloud.google.com/sql/
  • DigitalOcean: https://www.digitalocean.com/products/managed-databases/
  • Linode: https://www.linode.com/docs/databases/postgresql/create-a-highly-available-postgresql-cluster-using-patroni-and-haproxy/
  • Azure: https://azure.microsoft.com/en-us/services/postgresql/
  • Citus: https://techcrunch.com/2019/01/24/microsoft-acquires-citus-data
  • Self-managed high availability cluster: https://github.com/sorintlab/stolon

Who?

Everyone building any application that stores data should learn SQL. SQL is ubiquitous in every field/industry and the sooner you learn/master it, the higher your life-time return on time investment.

Learning how to use a relational database is a foundational skill for all of computer science and application development.

Being proficient in SQL will open the door to Data Science with SQL-on-Hadoop Apache Spark, Google BigQuery, Oracle and Teradata. In short, get really good at SQL! It's very useful.

What?

This tutorial covers 5 areas:

  1. What is PostgreSQL?
  2. How do I get started with PostgreSQL? (a fully functioning example!)
  3. What is Structured Query Language (SQL)? (lots of example queries!)
  4. How do I write my own SQL Queries?
  5. How do I deploy my own PostgreSQL-based Application?

Once you have covered these areas, you will know if PostgreSQL is "right" for your needs, or if you need to keep looking for a different way to store data.

Let's dive in!

1. What is PostgreSQL?

PostgreSQL (often shortened to simply "Postgres") is an advanced Relational DataBase Management System ("RDBMS"), that lets you efficiently and securely store any type of data. We will explain "Relational Database" in the context of our example below, so don't worry if it sounds like a buzzword soup.

Postgres has an emphasis on standards compliance and extensibility which means there are many plugins you can use to enhance it like PostGIS for mapping applications and entire projects built on top of it like TimescaleDB (a time-series database perfect for analytics) and AgensGraph (a graph database, great for modelling networks e.g a "social graph").

Structured Query Language (SQL) is the preferred means of interacting with data at any scale.

The only reason MySQL is still more widely used than Postgres can be summarised in one word: WordPress. WordPress has a firm grip on the CMS-based website market and it shows no sign of slowing down. If your goal is to build CMS-based websites, or the company you already work for uses WordPress, you should go for it! If you prefer a more general introduction to SQL, follow this tutorial! The knowledge you will gain by learning Postgres is 95%+ "transferable" to other SQL databases so don't worry about the differences between MySQL and Postgres for now. If you're curious, read: https://hackr.io/blog/postgresql-vs-mysql

How?

Installation

Before you get started with using PostgreSQL, you'll have to install it. Follow these steps to get started:

MacOS

  1. There are a couple of ways to install PostgreSQL. One of the easier ways to get started is with Postgres.app. Navigate to https://postgresapp.com/ and then click "Download": download

  2. Once it's finished downloading, double click on the file to unzip then move the PostgreSQL elephant icon into your applications folder. Double click the icon to launch the application.

  3. You should now see a new window launched with a list of servers to the left side of the window (if it's a fresh install, you should see one named PostgreSQL XX). If it shows anything else or an error props up, make sure you don't have any other instances of Postgres on your computer and reinstall. To fully reinstall follow these steps to delete data directories and preferences. Click on the button 'Initialize' (or 'Start' if you had already installed previously). download

  4. Run sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp (found here) to use psql in the terminal. Close and open the terminal.

  5. Postgres.app will by default create a role and database that matches your current macOS username. You can connect straight away by running psql.

  6. You should then see something in your terminal that looks like this (with your macOS username in front of the prompt rather than 'postgres'):

terminal

  1. You should now be all set up to start using PostgreSQL. For documentation on command line tools etc see https://postgresapp.com/documentation/

Ubuntu

Digital Ocean have got a great article on getting started with postgres. A quick summary is below.

Installation
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

By default the only role created is the default 'postgres', so PostgreSQL will only respond to connections from an Ubuntu user called 'postgres'. We need to pretend to be that user and create a role matching our actual Ubuntu username:

sudo -u postgres createuser --interactive

This command means 'run the command createuser --interactive as the user called "postgres"'.

When asked for the name of the role enter your Ubuntu username. If you're not sure, open a new Terminal tab and run whoami.

When asked if you want to make the role a superuser, type 'y'.

We now need to create the database matching the role name, as PostgreSQL expects this. Run:

sudo -u postgres createdb [your user name]

You can now connect to PostgreSQL by running psql.

Create your first PostgreSQL database

  1. To start PostgreSQL, type this command into the terminal:
    psql

  2. Next type this command into the PostgreSQL interface:
    CREATE DATABASE test;
    NOTE: Don't forget the semi-colon. If you do, useful error messages won't show up.

  3. To check that our database has been created, type \l into the psql prompt. You should see something like this in your terminal: test db

Create new users for your database

  1. If you closed the PostgreSQL server, start it again with:
    psql

  2. To create a new user, type the following into the psql prompt:
    sql CREATE USER testuser;

  3. Check that your user has been created. Type \du into the prompt. You should see something like this: user Users can be given certain permissions to access any given database you have created.

  4. Next we need to give our user permissions to access the test database we created above. Enter the following command into the psql prompt:
    sql GRANT ALL PRIVILEGES ON DATABASE test TO testuser;

PostGIS - Spacial and Geographic objects for PostgreSQL

PostGIS Installation

If you've installed Postgres App as in the example above, you can easily extend it to include PostGIS. Follow these steps to begin using PostGIS:

  1. Ensure that you're logged in as a user OTHER THAN postgres. Follow the steps above to enable your default user to be able to access the psql prompt. (installation step 7)

  2. Type the following into the psql prompt to add the extension:
    CREATE EXTENSION postgis;

PostGIS Distance between two sets of coordinates

After you've extended PostgreSQL with PostGIS you can begin to use it. Type the following command into the psql command line:

```sql SELECT ST_Distance(gg1, gg2) As spheroid_dist FROM (SELECT ST_GeogFromText('SRID=4326;POINT(-

Core symbols most depended-on inside this repo

connect
called by 9
server/db.js
insert_rows
called by 4
server/db.js
insert_next_page
called by 4
server/db.js
select_person
called by 3
server/db.js
profile_next_page
called by 2
server/db.js
fetch_list_of_profiles_slowly
called by 1
server/bot.js
select_org
called by 1
server/db.js
select_repo
called by 1
server/db.js

Shape

Function 20

Languages

TypeScript100%

Modules by API surface

server/db.js14 symbols
server/utils.js3 symbols
server/bot.js2 symbols
server/request_handlers.js1 symbols

Datastores touched

codefaceDatabase · 1 repos

For agents

$ claude mcp add learn-postgresql \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact