Weighing Choices: URI, Key-Value and JDBC Connection Strings
A connection string always carries the same basic information: which database, where it is, and how to log in. But it can be written in different shapes depending on your language and driver. Seeing the same details in each shape makes them much less confusing.
The details we will write three ways
| Detail | Value used in this example |
|---|---|
| Database type | PostgreSQL |
| Host | localhost |
| Port | 5432 |
| Database name | library |
| User | reader |
| Password | a placeholder, YOUR_PASSWORD |
Shape 1: URI (URL-style)
postgresql://reader:YOUR_PASSWORD@localhost:5432/library
It looks like a web address. Many Python, Node.js, Ruby and Go libraries accept this form, and hosting platforms often hand you a URI in a variable called DATABASE_URL.
Good to know: special characters in the password, such as @ or /, must be "percent-encoded" or the string will be misread.
Shape 2: key-value pairs
host=localhost port=5432 dbname=library user=reader password=YOUR_PASSWORD
PostgreSQL's own client library understands this form. SQL Server and .NET apps use a similar style with semicolons:
Server=localhost,1433;Database=library;User Id=reader;Password=YOUR_PASSWORD;
Good to know: each piece has a clear label, which makes it easier to read and edit.
Shape 3: JDBC URL
jdbc:postgresql://localhost:5432/library
Java programs use JDBC (Java Database Connectivity). The URL usually leaves out the user and password; they are passed separately in code or configuration.
Good to know: extra options go after a ?, such as ?sslmode=require.
Side by side
| Shape | Who uses it | Password inside? | Easy to read? |
|---|---|---|---|
| URI | Many scripting languages, hosting platforms | Often, yes | Compact, but symbols can trip you up |
| Key-value | PostgreSQL tools, .NET and SQL Server | Yes | Very readable |
| JDBC URL | Java and other JVM languages | Usually not | Readable, credentials kept apart |
How to choose
You usually do not choose freely: your driver's documentation says which shapes it accepts. Follow that. If several are allowed, pick the one your team or tutorial already uses, so examples match.
The rule that applies to every shape
Do not write the real password into your source code. Store the whole string, or at least the password, in an environment variable. Our page on keeping a connection string in an environment variable shows how, and the connection string generator can produce each format for you to compare.
A quick self-test
Look at this string and name each part: mysql://app:SECRET@db.example.com:3306/shop. (Answer: database type MySQL, user app, password SECRET, host db.example.com, port 3306, database shop.) If you got them all, you can read almost any connection string.
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.