A beginner-friendly guide to the world's most advanced open-source relational database โ from install to your first query
PostgreSQL (often just "Postgres") is a powerful, open-source relational database. It stores your data in tables of rows and columns, and lets you ask questions about that data using SQL (Structured Query Language). It has built everything from tiny hobby projects to the world's largest websites โ and it's completely free.
| Scenario | Why PostgreSQL |
|---|---|
| Modern web apps | Used inside Django, Rails, Next.js stacks as the primary database |
| Geospatial | The PostGIS extension powers map-based apps |
| Analytics & BI | Handles complex aggregate queries over large data sets |
| JSON documents | Stores and queries JSON natively โ a document store and relational DB in one |
| Feature | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
| Type | Full client-server RDBMS | Full client-server RDBMS | Embedded file-based database |
| Best for | Complex apps, analytics, serious data integrity | CRUD web apps, MySQL-aware stacks | Small local apps, tests, prototypes |
| Server required | Yes | Yes | No โ a file on your disk |
| JSON support | Excellent (native) | Good (added later) | Limited |
| Licence | PostgreSQL Licence (free) | GPL/dual licence | Public domain |
| Term | What it is |
|---|---|
| Database | A named container that holds all your data (a separate library) |
| Table | Data arranged in rows and columns, like a spreadsheet sheet |
| Row (record) | One entry in a table โ e.g. one customer |
| Column (field) | A single attribute across all rows โ e.g. email |
| Primary key | A unique ID for each row โ no duplicates allowed |
| Foreign key | A column pointing to another table, linking data up |
| Query | A SQL statement that retrieves or changes data |
| Schema | A namespace inside a database (organises tables, defaults to "public") |
On Debian/Ubuntu-based distributions, PostgreSQL is packaged in the official repositories. The steps below install the server, the client tools, and contrib extensions.
$ sudo apt update $ sudo apt install -y postgresql postgresql-contrib
Ubuntu also offers an postgresql-16 package name; generic postgresql picks the default version. On RHEL/Fedora: sudo dnf install postgresql-server and initialise with sudo postgresql-setup --initdb.
$ sudo systemctl enable --now postgresql $ systemctl status postgresql # should show active (running)
PostgreSQL creates an OS user called postgres. To connect, switch to that user and start the client:
$ sudo -iu postgres $ psql postgres=# \l
\l lists databases โ that's your inventory. Press q to quit lists, and type \q to leave psql.
Head to the official downloads at postgresql.org/download/windows and choose the installer for your version (e.g. 16 or 17). It bundles pgAdmin (a GUI tool) too.
Accept defaults. You'll pick a data directory, select a port (default 5432), and โ importantly โ set the password for the postgres superuser.
If asked, you can skip Stack Builder (extra tools). Everything you need is already installed.
> set PATH=%PATH%;C:\Program Files\PostgreSQL\16\bin
Then you can type psql from any terminal instead of the full path. (The exact folder depends on the version you picked.)
> psql -U postgres
Password for user postgres: ********
It will ask for the password you set during install. This lands you at the same postgres=# prompt as on Linux.
Create a scratch database, a table, add data, and read it back โ this proves your server works.
-- at the postgres=# prompt CREATE DATABASE myshop; postgres=# \c myshop -- connect to the new database -- create a simple table CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, price NUMERIC ); -- insert a couple of rows INSERT INTO products (name, price) VALUES ('Notebook', 4.50), ('Pen', 1.25); -- query them back SELECT * FROM products;
Expected output โ a table with our two rows and an auto-generated id for each.
CREATE TABLE is adding a new sheet with named columns.
INSERT is typing a row, and SELECT is filtering/sorting that sheet.
The difference: PostgreSQL checks every rule (types, unique IDs, links) and handles concurrent users
and data safety for the whole organisation.
| Action | Linux (systemctl) | Windows |
|---|---|---|
| Start / stop | sudo systemctl start postgresql / stop | via Services app or net start postgresql-x64-16 |
| Enabled on boot | sudo systemctl enable postgresql | Installer adds it as a Windows service by default |
| Config files | /etc/postgresql/*/main/ | C:\Program Files\PostgreSQL\16\data\ |
| Logs | sudo journalctl -u postgresql | data\postgresql-16.log |
By default Postgres only listens on localhost โ good for security. To allow other machines, edit
postgresql.conf (set listen_addresses = '*') and pg_hba.conf to add a
connection rule. Advanced but useful the day you expose a database.
| Command | What it does |
|---|---|
psql -d dbname | Connect to a database |
\l | List databases |
\dt | List tables in the current database |
\d tablename | Describe a table's structure |
\q | Quit psql |
pg_dump dbname | Back up a database to a file |
pg_restore | Restore a backup |
CREATE DATABASE x; | Create a new database |
BEGIN ... COMMIT โ all or nothing.
SELECT mastery.
PostgreSQL is the database that keeps your data safe and your queries fast โ free forever.
Install it, create a table, run a query, and you're on your way to serious data work.
Created by jcmatira