Reading a Connection String Piece by Piece

A connection string is the address your program uses to find and log in to a database. It looks like line noise at first. Once you know its parts, you can read any of them. This walkthrough takes one apart slowly.

Designing related tables illustration

Step 1: look at the whole thing

postgresql://maria:pa55word@localhost:5432/school?sslmode=require

This is the URL style, used by many libraries. It has the same shape as a web address, and that is no accident.

Step 2: take it apart

PieceIn our exampleWhat it means
Schemepostgresql://Which kind of database to talk to
UsermariaThe account to log in as
Passwordpa55wordThat account's password
HostlocalhostWhich computer the database runs on; localhost means "this computer"
Port5432Which "door" on that computer the database listens at
DatabaseschoolWhich database on the server to use
Optionssslmode=requireExtra settings, here "use an encrypted connection"

The @ separates the login details from the address. The ? starts the options, and & separates several options.

Step 3: meet the common default ports

You often leave the port out and the default is used:

Step 4: the same idea in other styles

Not every library uses URLs. Here is the same kind of information in two other common forms:

jdbc:postgresql://localhost:5432/school
Server=localhost;Database=school;User Id=maria;Password=pa55word;

The first is the Java (JDBC) style; the user and password usually go in separately. The second is the key=value style used by .NET libraries for SQL Server. Different punctuation, same ingredients.

Step 5: SQLite is different

SQLite has no server, so there is no host, port or password. The "connection string" is just the path to the file:

sqlite:///practice.db

Some libraries want only the file name, such as practice.db.

A worked example

You are given this string by a teacher for a class project:

mysql://student1@db.example.edu:3306/library

Reading it: a MySQL database, logging in as student1, no password in the string (it will be asked for or supplied separately), on the computer db.example.edu, at the standard MySQL port, using the library database.

Common mistakes

  1. Special characters in passwords. A password containing @ or / confuses the URL. Such characters must be encoded (for example @ becomes %40), or the password supplied separately.
  2. Wrong host from inside containers. In Docker, localhost means the container itself, not your laptop.
  3. Putting the string in your code. Anyone who sees the code sees the password. Our notes on keeping connection strings secret show the better way.
  4. Mixing up database and server. One server can hold many databases; the name at the end picks one.

Want to build one without typing punctuation? Try our connection string tool.

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.

This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.

Related pages on this site