Before we start writing queries, let us get PostgreSQL running on your machine. This lesson will walk you through the setup process. By the end, you will have a working PostgreSQL installation and be ready to create your first database.
Using the Online Compiler
The quickest way to get started is to use our online PostgreSQL compiler. No installation needed. You can write and run SQL queries right in your browser.
This is perfect for learning and experimenting. However, if you want to build real projects, you will eventually need a local installation. Let us cover that next.
Local Installation
PostgreSQL runs on every major operating system. Here is how to install it:
Windows
Download the installer from the official PostgreSQL website. The installer includes pgAdmin, a graphical tool for managing your databases. During installation, you will be asked to set a password for the superuser (postgres). Remember this password — you will need it.
macOS
The easiest way on macOS is to use Homebrew:
brew install postgresql@16
brew services start postgresql@16
This installs PostgreSQL and starts it as a background service. You can connect immediately.
Linux (Ubuntu/Debian)
Use the package manager:
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
After installation, PostgreSQL creates a system user called postgres. Switch to
that user to access the database:
sudo -i -u postgres
Connecting to PostgreSQL
Once installed, you connect using the psql command-line tool:
psql -U postgres
You will be prompted for the password you set during installation. If everything works, you
will see a prompt that looks like postgres=#. That means you are connected and
ready to go.
To quit psql, type:
\q
Verifying the Installation
Run this quick check to make sure everything is working:
SELECT version();
This will show you the PostgreSQL version and some details about your system. If you see output, congratulations — PostgreSQL is installed and running.