Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard DSN format?

Tags:

php

pdo

PHP PDO uses this style:

MySQL

mysql:host=hostname;dbname=databasename with username and password in their own separate parameters

PostgreSQL

pgsql:host=hostname;dbname=databasename;user=username;password=password

But then when search Github for a DSN parser, they seem to use a different format altogether, like this:

mysql://user:pass@hostname:port/databasename

My question is, is there actually a standard DSN format that every language accepts?

like image 694
IMB Avatar asked Sep 18 '25 15:09

IMB


1 Answers

Let's see what is actually DSN?

DSN stands for Data Source Name. But this is also known as Database Source Name. DSN term also overlaps with connection string. Most systems do not make a distinction between DSNs or connection strings and the term can often be used interchangeably.

DSN attributes may include, but are not limited to:

  1. the name of the data source
  2. the location of the data source
  3. the name of a database driver which can access the data source
  4. a user ID for data access (if required)
  5. a user password for data access (if required)

The information is above taken from wiki

In general, the following format of a connection string is maintained by many languages for database connections. This may be a community standard. But I didn't see any specification of DSN.

scheme://username:password@host:port/dbname?param1=value1&param2=value2&...

There are several ways to connect to a database. So there are some common attributes that are used for all types of database connections. See the following examples:

Connect to database through a socket

mysql://user@unix(/path/to/socket)/dbname

Connect to database on a non standard port

pgsql://user:pass@tcp(localhost:5555)/dbname

Connect to SQLite on a Unix machine using options

sqlite:////full/unix/path/to/file.db?mode=0666

Connect to SQLite on a Windows machine using options

sqlite:///c:/full/windows/path/to/file.db?mode=0666

Connect to MySQLi using SSL

mysqli://user:pass@localhost/pear?key=client-key.pem&cert=client-cert.pem

Connecting to MS Access sometimes requires admin as the user name

odbc(access)://admin@/datasourcename

Connecting to ODBC with a specific cursor

odbc(access)://admin@/datasourcename?cursor=SQL_CUR_USE_ODBC

Here I am providing two links for PostgreSQL and MySQL respectively. You will find there how they use connection string to connect a database.

  • Connect PostgreSQL using connection string
  • Connect MySQL using connection string
like image 66
unclexo Avatar answered Sep 21 '25 05:09

unclexo