Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psql shortcut for frequently used queries? (like Unix "alias")

Is it possible to somehow create aliases (like Unix alias command) in psql?

I mean, not SQL FUNCTION, but local aliases to ease manual queries?

like image 267
lajarre Avatar asked Apr 25 '13 23:04

lajarre


People also ask

What is 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 D in psql?

psql -d postgres. here -d stands for database name and hence postgres is your database name. You can open the terminal by only typing psql , Above statement will do two things for you. Open psql terminal. Connect you to postgres db.

How do I run multiple PostgreSQL statements?

Batch mode You can write multiple psql commands and SQL statements in one text file (say, named statements. sql ), and then use the command psql congress -af statements. sql to execute them all. Use “ ; ” to signal the end of each SQL statement in the file.

Does postgres optimize queries?

How the PostgreSQL query optimizer works. Just like any advanced relational database, PostgreSQL uses a cost-based query optimizer that tries to turn your SQL queries into something efficient that executes in as little time as possible.


1 Answers

I don't know about any possibility. There is only workaround for psql based on psql variables, but there is lot of limits - using parameters for this queries is difficult.

postgres=# \set whoami 'SELECT CURRENT_USER;'
postgres=# :whoami
 current_user 
--------------
pavel
(1 row)
like image 61
Pavel Stehule Avatar answered Nov 14 '22 04:11

Pavel Stehule