Install PostgreSQL and Connectπ± Beginner
To start using PostgreSQL locally, you need to install the database server and connect to it using a client like psql, the ultra-fast built-in command-line tool.
What is psql?
psql is the interactive terminal specifically designed for PostgreSQL. While Graphical User Interfaces (GUIs) like pgAdmin or DBeaver are great, psql is installed universally on all servers and is the fastest way to run SQL queries or database maintenance tasks directly from your terminal.
Why Use Docker for Installation?
Installing a database directly on your operating system can lead to messy background services and conflicting ports. Running PostgreSQL inside a Docker container is the industry standard for local development because it isolates the database and makes it trivial to start, stop, or delete.
How to Install and Connect
First, start a PostgreSQL 18 container using Docker:
# Run a postgres instance mapped to default port 5432
docker run --name pg-dev -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:18Next, use a connection string to connect to the database. By default, Postgres listens on port 5432.
# Connect using the standard connection URL format
psql postgresql://postgres:secret@localhost:5432/postgresHow to Navigate psql
Once inside the psql terminal, you can run standard SQL or use special "meta-commands" (starting with a backslash) to inspect the server:
| Command | What it Does |
|---|---|
\l | Lists all databases on the server. |
\c mydb | Connects you to a specific database named 'mydb'. |
\dt | Lists all tables in the current database. |
\d users | Describes the schema (columns/types) of the 'users' table. |
\q | Quits the psql terminal and returns to bash. |
postgresql://USER:PASSWORD@HOST:PORT/DATABASE. Keep this URL secured in your .env file.