brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: \conninfo in psql shows which database, user and host you are on

Database
~/tips/tip-database-psql-conninfo

postgres=# \conninfo

Prints the details of the current psql connection, so you know where a query is about to run.

Production and test look alike in a terminal. Before you run something that changes data, ask psql where you actually are.

postgres=# \conninfo

The psql documentation says \conninfo outputs information about the current database connection, including SSL information when SSL is in use. You see the database, the user, the host or socket and the port.

Older versions print one sentence along the lines of: You are connected to database “app” as user “me” on host “localhost” at port “5432”. Newer releases show the same facts in a table. The current manual’s description mentions fields such as Client User and Superuser, and notes that those can differ if you switched with SET ROLE. So expect the layout to depend on your psql version.

A good habit is to run it right after you connect, and again after a \c to another database. For the user and database names alone there are plain queries too:

SELECT current_database(), current_user;

I like having \conninfo in muscle memory, because it costs a second and answers the one question that matters before a DELETE.

The caveat is that it reports the connection of this psql session only, and it says nothing about what the data in that database is. A database called app_test on the production server is still production. Check the host as well as the name.

Further reading: PostgreSQL documentation: psql

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link