Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSQL connection alias

Is it possible to create a connection alias for psql?

For example, instead of having to type out:

psql --host=pr-gis-db --username=peregrinius --port=5432

I just type

psql pr-gis-db

And it connects with pre-configured settings.. Maybe prompts for password instead of storing it.

like image 782
James Burke Avatar asked Aug 23 '16 09:08

James Burke


People also ask

Can we use alias in PostgreSQL?

In PostgreSQL, an alias is a temporary alternative name for columns, tables, views, materialized views, etc. in a query. Aliases are assigned during query execution and aren't stored in the database or on disk. By using column aliases, the query output can become more meaningful.

What is alias in PostgreSQL?

The PostgreSQL Alias is used to assign a temporary name to a table or a column in a query. They only exist at the time of the query execution.

Does psql use TCP?

PostgreSQL uses a message-based protocol for communication between frontends and backends (clients and servers). The protocol is supported over TCP/IP and also over Unix-domain sockets.


1 Answers

following the instructions here: https://www.postgresql.org/docs/current/static/libpq-pgservice.html

Create/update the file ~/.pg_service.conf

[pr-gis-db]
host=pr-gis-db
user=peregrinius
dbname=gis
port=5432

to run the alias connection

psql service=pr-gis-db

Note: you can also overwrite parameters in the file if you specify them when calling the service.

psql service=pr-gis-db -U dbadmin
like image 90
James Burke Avatar answered Sep 23 '22 18:09

James Burke