What a Database Server Is, and What Localhost Means

SQLite is a file. PostgreSQL and MySQL are servers. That difference confuses many beginners, along with words like localhost and port. This page untangles them.

A server is a program that waits

A database server is a program that runs in the background, waiting for other programs to connect and send queries. PostgreSQL, MySQL and SQL Server all work this way. Your app is a client: it connects to the server, sends SQL and receives results.

SQLite is different. There is no separate program; the SQLite library inside your app reads and writes the database file directly. That is why SQLite needs no installation or password.

Host: which computer?

To connect to a server, your app needs to know which computer it runs on. That is the host.

Port: which door?

One computer can run many server programs, so each listens on a numbered port. The usual defaults:

DatabaseDefault port
PostgreSQL5432
MySQL3306
SQL Server1433

If your server uses the default, you can often leave the port out of the connection settings.

Putting it together

A connection string combines these with a user, password and database name:

postgresql://learner:secret@localhost:5432/practice

Read it as: connect as user learner, with password secret, to the server on this computer at port 5432, and use the database called practice. In real projects, keep the password out of code; see keeping a connection string in an environment variable.

Why "connection refused" happens

This error means nothing answered at that host and port. Common reasons:

  1. The server is not running. Start it from your system's services or the database's installer tools.
  2. The port is different from what you typed.
  3. The host is wrong, for example localhost when the database runs inside a container or on another machine.

One server, many databases

A single PostgreSQL or MySQL server can hold several separate databases, such as practice and homework. The database name in your connection settings picks one. A common mistake is creating tables in one database and querying another.

A quick self-check

QuestionAnswer for SQLiteAnswer for PostgreSQL on your laptop
Is there a server program?NoYes
What is the host?Not needed; you give a file pathlocalhost
What is the port?Not needed5432 by default
Do I need a password?NoUsually yes

For a gentle walk through every part of a connection string, see reading a connection string piece by piece.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Related pages on this site