TigerData logo
TigerData logo
  • Product

    Tiger Cloud

    Robust elastic cloud platform for startups and enterprises

    Agentic Postgres

    Postgres for Agents

    TimescaleDB

    Postgres for time-series, real-time analytics and events

  • Docs
  • Pricing

    Pricing

    Enterprise Tier

  • Developer Hub

    Changelog

    Benchmarks

    Blog

    Community

    Customer Stories

    Events

    Support

    Integrations

    Launch Hub

  • Company

    Contact us

    About

    Timescale

    Partners

    Security

    Careers

Log InTry for free

Products

Time Series and Analytics AI and Vector Enterprise Plan Cloud Status Support Security Cloud Terms of Service

Learn

Documentation Blog Forum Tutorials Changelog Success Stories Time Series Database

Company

Contact Us Careers About Brand Community Code Of Conduct Events

Subscribe to the Tiger Data Newsletter

By submitting, you acknowledge Tiger Data's Privacy Policy

2026 (c) Timescale, Inc., d/b/a Tiger Data. All rights reserved.

Privacy preferences
LegalPrivacySitemap

Categories

All posts

AI

Analytics

Announcements & Releases

Benchmarks & Comparisons

Data Visualization

Developer Q&A

Engineering

General

IoT

Open Source

PostgreSQL

PostgreSQL Performance

PostgreSQL Tips

State of PostgreSQL

Time Series Data

Tutorials

Category: All posts

PostgreSQL, Blog

Feb 09, 2024

Connecting to Postgres With psql and .pg_service.conf

An inquisitive tiger with neon databases in the background. Learn how to connect to Postgres with psql and .pg_service.conf

Posted by

James Blackwood-Sewell

01

Goodbye, psql -h; hello, .pg_service.conf

02

And goodbye, .pg_services.conf; hello, database connection config button

Every now and then, I stumble upon random Postgres features that I’ve simply forgotten about. In this case, it’s somewhat poetic that the very thing I’d forgotten about (.pg_service.conf) is something that ensures I won’t forget how to connect to my database services ever again.

It probably comes as no surprise that the Developer Advocacy project on Timescale contains a lot of databases. Every database has a name, but unfortunately, you need to know the hostname and the port they are exposed on to connect to them. If you’re a Timescale user, you can get that information from the Timescale Console, but when you’re jumping between instances a lot, that becomes pretty tiresome.

Goodbye, psql -h; hello, .pg_service.conf

In the past, from a terminal, I’ve relied on searching my history for the correct psql command, which works but can be pretty hit-and-miss. Sometimes I can’t remember if I need to connect to g10hmvdlg9.jj7sbwx9nt.tsdb.cloud.timescale.com:37525 or jb4cnq3gyf.jj7sbwx9nt.tsdb.cloud.timescale.com:42188 because, sadly, I don’t have a photographic memory. I do know that I need to connect to my big_benchmark machine, so off to the Console I go to look it up.

A while ago, I remembered about the .pg_service.conf file, which lets you alias names with PostgreSQL connection information. I quickly made myself a file that looked like this:

[big_benchmark]
host=g10hmvdlg9.jj7sbwx9nt.tsdb.cloud.timescale.com
port=37525
dbname=tsdb
user=tsdbadmin

The command line psql client knows about this file, so now I can use the magic service flag to connect to my big_benchmark service directly:

$ psql service=big_benchmark

Password for user tsdbadmin: 
psql (15.4, server 15.5 (Ubuntu 15.5-1.pgdg22.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

tsdb=> 

And goodbye, .pg_services.conf; hello, database connection config button

Date published

Feb 09, 2024

Posted by

James Blackwood-Sewell

Get Started Free with Tiger CLI

Date published

Feb 09, 2024

Posted by

James Blackwood-Sewell

Get Started Free with Tiger CLI

Subscribe to the Tiger Data Newsletter

By submitting you acknowledge Tiger Data's Privacy Policy.

Share

Subscribe to the Tiger Data Newsletter

By submitting you acknowledge Tiger Data's Privacy Policy.

Share

Subscribe to the Tiger Data Newsletter

By submitting you acknowledge Tiger Data's Privacy Policy.

Amazing! But looking at the list of services I needed to add to my .pg_services.conf file was daunting—what if the Timescale Console could do this for me? I posted the idea to our front-end team’s Slack channel, and a few weeks later, a new button appeared:

image

Clicking on the link downloads a file that contains the configuration information for all my Timescale services, as well as some instructions for installing it on your system:

# This file includes configuration for all current services in your project
# To use it from psql either move the downloaded file to ~/.pg_service.conf
# or set the PGSERVICECONF environment variable to point at it.
# You will then be able to run 'psql service=service_name' to connect!

I’ve been using this new workflow every day. If you’re not on Timescale, then I’d encourage you to build your own .pg_service.conf file, and if you are, then let us make one for you! You can create a free Timescale account in just a couple of minutes, time you will surely win back with our connection database configuration link.