668

What is the format for the PostgreSQL connection string (URL postgres://...) when the host is not the localhost?

4

9 Answers 9

972

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
8
  • 86
    This workded for me postgres://user:secret@localhost:5432/mydatabasename
    – George
    Feb 16, 2018 at 15:04
  • 12
    postgresql://localhost/mydb?user=other&password=secret did the trick
    – karuhanga
    Oct 29, 2018 at 14:31
  • 1
    If 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, 2019 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, 2019 at 18:07
  • 11
    To 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, 2020 at 11:28
241

The following worked for me

const conString = "postgres://YourUserName:YourPassword@YourHostname:5432/YourDatabaseName";
5
  • How to use this connection string in ruby? Jul 14, 2020 at 10:18
  • 12
    is 'postgres://' and 'postgresql://' interchangeable ?
    – Ryu S.
    Mar 21, 2021 at 1:47
  • 6
    @RyuS. The URI scheme designator can be either postgresql:// or postgres:// From here: postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING Jun 26, 2021 at 21:13
  • 1
    In case your hostname is a url, you can use ping hostname to get IP address.
    – Akansh
    Sep 3, 2021 at 8:14
  • Thank you this is the simpliest answer. Thanks @Hemadri Dasari
    – Two
    Nov 20 at 10:34
125
DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}
38

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.

3
  • 2
    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, 2020 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, 2021 at 20:59
  • First link is broken (JDBC documentation)
    – tomermes
    Dec 7, 2022 at 15:17
11

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;
5
  • The general format of database url
DATABASE_URL=postgresql://username:password@host:port/dtabase_name
  • If you are using postgresql sql with asyncpg the database url would be
DATABASE_URL=postgresql+asyncpg://username:password@host:port/dtabase_name
  • Remember to never push your database password so you should use your DATABASE_URL in .env file
  • The port is optional if you use the default one
  • Like this you can connect both local and remote database think of that once you want to check an issue that occur in the remote deployed versions

  • ex of localhost DATABASE_URL would be

DATABASE_URL=postgresql+asyncpg://postgres:dina@localhost/mysens
  • If you deployed your database on Heroku and you want to connect it with your local app, go to Heroku Postgres installed add-on go to settings and click on view credential in Database Credentials and use the uri to connect to your database
DATABASE_URL=postgresql+asyncpg://sqnalxxxxxxxxx:160xxxx2bdd2942b26c93c392xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[email protected]:5432/del6o4cjoqfsov

enter image description here

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}
3
  • Not valid way to define connection URI May 26, 2022 at 13:04
  • This is not a valid approach for Postgress
    – Golinmarq
    Aug 29, 2022 at 22:47
  • What about database name? Why database is on the same port as the server?
    – parsecer
    Apr 27 at 15:42
4

The connection string can also be retrieved programmatically from working DB connectors.

For instance I sometimes extract connection strings from SQLAlchemy's engine, like this:

> db_engine.url
postgres://{user}:{password}@{host}:{port}/{db_name}?sslmode=require
0

Some people seem to misread the database name as a server name and the host as a postgresql server? A host hosts a postgresql server that has a database. Or am I missing something.

postgresql://my_host/&server=my_postgresql_server?user=my_user&port=my_port&password=my_password&database=my_database

Example:

my_host: can be "localhost" (but that is not in the question) or an ip address of a host.

postgresql://my_host/&server=postgres?user=postgres&port=5432&password=postgres&database=test_db

Worked for me in Python with sqlalchemy and a postgresql localhost running. Needs sqlalchemy, postgresql, and psycopg2 to get it to work.

PS: The question is about a postgres://... URL, but this would not work here. Instead, you need postgresql, and what is run in the end in Python is dialect+driver (see Database URLs) = postgresql+psycopg2, without having to write it like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.