TigerData logo
TigerData logo
  • Product

    Product

    Tiger Cloud

    Robust elastic cloud platform for startups and enterprises

    TimescaleDB Enterprise

    Self-managed TimescaleDB for on-prem, edge and private cloud

    Open source

    TimescaleDB

    Time-series, real-time analytics and events on Postgres

    Search

    Vector and keyword search on Postgres

  • Industry

    Data Centers

    Energy & Utilities

    Oil & Gas Operations

    Smart Manufacturing

    Crypto

  • Docs
  • Pricing
  • Developer Hub

    Changelog

    Benchmarks

    Blog

    Community

    Customer Stories

    Events

    Support

    Integrations

    Launch Hub

  • Company

    About

    TigerData logo

    Timescale

    Partners

    Security

    Careers

Contact usStart a free trial
Tiger Data

Products

  • TimescaleDB
  • Tiger Cloud
  • TimescaleDB Enterprise
  • Postgres Search Stack

Industry

  • Data Centers
  • Energy & Utilities
  • Oil & Gas Operations
  • Smart Manufacturing
  • Crypto

Support

  • Cloud Status
  • Support
  • Security
  • Terms of Service
  • Code Of Conduct

Learn

  • Documentation
  • Blog
  • Tutorials
  • Changelog
  • Success Stories

Company

  • About
  • Contact Us
  • Careers
  • Newsroom
  • Brand
  • Events

Products

  • TimescaleDB
  • Tiger Cloud
  • TimescaleDB Enterprise
  • Postgres Search Stack

Industry

  • Data Centers
  • Energy & Utilities
  • Oil & Gas Operations
  • Smart Manufacturing
  • Crypto

Support

  • Cloud Status
  • Support
  • Security
  • Terms of Service
  • Code Of Conduct

Learn

  • Documentation
  • Blog
  • Tutorials
  • Changelog
  • Success Stories

Company

  • About
  • Contact Us
  • Careers
  • Newsroom
  • Brand
  • Events
Privacy preferencesLegalPrivacySitemap

Subscribe to the Tiger Data newsletter

Gold Partner with Inductive Automation — Ignition

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

Tiger Data
GOLD PARTNER WITHINDUCTIVE AUTOMATION

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

Privacy preferencesLegalPrivacySitemap

Connecting to Postgres With psql and .pg_service.conf

J

By James Blackwood-Sewell

February 9th, 2024

2 min

Share

J

By James Blackwood-Sewell

February 9th, 2024

2 min

Share

Copy as HTML

Open in ChatGPT

Open in Claude

Open in v0

PostgreSQL, Blog

PostgreSQL Tips

Table of contents

  1. 01 Goodbye, psql -h; hello, .pg_service.conf
  2. 02 And goodbye, .pg_services.conf; hello, database connection config button
Get started for free
An inquisitive tiger with neon databases in the background. Learn how to connect to Postgres with psql and .pg_service.conf

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

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.

// Related posts

Speed Up Your Workflows: Introducing SQL Assistant, Recommendations, and Insights
Speed Up Your Workflows: Introducing SQL Assistant, Recommendations, and Insights

Announcements & Releases

PostgreSQL, Blog

Speed Up Your Workflows: Introducing SQL Assistant, Recommendations, and Insights

Build features, not workarounds. Our developer tools keep you shipping: introducing SQL Assistant, Recommendations, and Insights.

By Grant Godeke

May 1st, 2025

We Listened: Pgai Vectorizer Now Works With Any Postgres Database
We Listened: Pgai Vectorizer Now Works With Any Postgres Database

Announcements & Releases

AI

We Listened: Pgai Vectorizer Now Works With Any Postgres Database

By popular demand, pgai Vectorizer is now a Python library and CLI that works with any self-hosted or managed Postgres database. See how we built it.

By Avthar Sewrathan

April 30th, 2025

Connecting S3 and Postgres: Automatic Synchronization Without ETL Pipelines
Connecting S3 and Postgres: Automatic Synchronization Without ETL Pipelines

Announcements & Releases

AI

Connecting S3 and Postgres: Automatic Synchronization Without ETL Pipelines

Running analytical queries on Parquet files? Searching unstructured documents embeddings with pgvector? Get Postgres performance & S3 scalability in one place.

By Vineeth Pothulapati

April 30th, 2025

Stay updated with new
posts and releases.

Receive the latest technical articles and release notes in your inbox.