What is the format for the PostgreSQL connection string (URL postgres://...) when the host is not the localhost?
Asked
Active
Viewed 5.4e+01k times
471
Rob Bednark
- 22,937
- 20
- 77
- 112
JII
- 4,797
- 3
- 17
- 9
-
1This link provides information about connection string, driver class, and driver library. https://docs.oracle.com/cd/E19509-01/820-3497/agqka/index.html Also to download the recent jar files, use this link: https://jdbc.postgresql.org/download.html – Forgotten Ape Aug 02 '19 at 22:21
-
This might be useful: https://www.prisma.io/docs/concepts/database-connectors/postgresql#connection-details – Aniruddha Oct 26 '21 at 10:03
-
e.g `postgres://postgres:123456@127.0.0.1:5432/dummy` – Eric Mar 02 '22 at 08:47
-
Related question about pgAdmin: https://stackoverflow.com/questions/61479570/how-to-connect-to-database-through-jdbc-in-pgadmin#comment125419866_62014740 – Ryan Apr 19 '22 at 18:30
6 Answers
727
If you use Libpq binding for respective language, according to its documentation URI is formed as follows:
postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]
Here are examples from same document
postgresql://
postgresql://localhost
postgresql://localhost:5432
postgresql://localhost/mydb
postgresql://user@localhost
postgresql://user:secret@localhost
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
postgresql://localhost/mydb?user=other&password=secret
Mike 'Pomax' Kamermans
- 44,582
- 14
- 95
- 135
Andrey
- 8,022
- 1
- 15
- 10
-
66This workded for me postgres://user:secret@localhost:5432/mydatabasename – George Livingston Feb 16 '18 at 15:04
-
7`postgresql://localhost/mydb?user=other&password=secret` did the trick – Karuhanga Oct 29 '18 at 14:31
-
1If you still have problems, check the special characters in your password, change it temporarily for only numbers and test the URL (just to validate that your connection is working as expected) – Edenshaw Feb 21 '19 at 16:29
-
My issue was to simply copying the "jdbc:postgres:// ..." string out of DataGrip. Unfortunately the error message did not help. Thank you! – barfoos Jun 13 '19 at 18:07
-
6To add to @Edenshaw's note on special characters, the password needs to be url encoded, I just stumbled upon this problem with a password containing the '@' character (replacing it with %40 solved it) – Tom Hemmes Jun 30 '20 at 11:28
-
Weirdly enough, I think it's supposed to be `ApplicationName` and not `application_name`. I tried a `show application_name;` query and it only used the application name I supplied if I supplied it as `ApplicationName=whatever` in the connection string. – Dave Yarwood Sep 28 '21 at 14:12
-
Strange, i could only get the jdbc:postgresql://localhost:5432/DB?user=X&password=Y syntax to work, not the user:password@host. Using version postgresql-42.3.3.jar – siggemannen Mar 15 '22 at 12:35
188
The following worked for me
const conString = "postgres://YourUserName:YourPassword@YourHostname:5432/YourDatabaseName";
Hemadri Dasari
- 29,321
- 31
- 106
- 146
-
-
5
-
1@RyuS. `The URI scheme designator can be either postgresql:// or postgres://` From here: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING – Grant Oberhauser Jun 26 '21 at 21:13
-
1In case your hostname is a url, you can use `ping hostname` to get IP address. – Akansh Sep 03 '21 at 08:14
28
Here is the documentation for JDBC, the general URL is "jdbc:postgresql://host:port/database"
Chapter 3 here documents the ADO.NET connection string,
the general connection string is Server=host;Port=5432;User Id=username;Password=secret;Database=databasename;
PHP documentation us here, the general connection string is
host=hostname port=5432 dbname=databasename user=username password=secret
If you're using something else, you'll have to tell us.
nos
- 215,098
- 54
- 400
- 488
-
Thanks. The ADO.NET format is also what you need to pass to `UseNpgsql()` for Entity Framework Core. I was a little confused whether it should be that or the postgres:// URL (which I've also seen as "postgresql://") – CrazyPyro Oct 21 '20 at 12:44
-
libpq (offical postgresql client lib) understands both URL form and name=value pairs form. if you connection is not ultimately through libpq then consult the documentation for your framework. – Jasen Nov 30 '21 at 20:59
10
the connection url for postgres syntax:
"Server=host ipaddress;Port=5432;Database=dbname;User Id=userid;Password=password;
example:
"Server=192.168.1.163;Port=5432;Database=postgres;User Id=postgres;Password=root;
Vinoth Shankar
- 660
- 8
- 9
4
server.address=10.20.20.10
server.port=8080
database.user=username
database.password=password
spring.datasource.url=jdbc:postgresql://${server.address}/${server.port}?user=${database.user}&password=${database.password}
Alan
- 180
- 2
- 9