Psql command line tutorial and cheat sheet
tomcam.github.io
tomcam.github.io
Even as someone already comfortable with psql, pgcli is extremely handy.
SELECT *
FROM parent
JOIN child USING (user_id)
I personally think naming your primary keys with their full name instead of calling all of them `id` or `key` is better practice also.and it works great with pspg pager [https://github.com/okbob/pspg]
''\pager /usr/local/bin/pspg''
From the manual:
=> SELECT 'hello' AS var1, 10 AS var2
-> \gset
=> \echo :var1 :var2
hello 10
testdb=> \set content `cat my_file.txt`
testdb=> INSERT INTO my_table VALUES (:'content');It's so inconvenient compared to psql, that it makes me cringe every time I have to work with MySQL. Garbled formatting of long statements, almost non-working history, editing long statements is cumbersome, etc.
Does MySQL authors use something else to communicate with the database?
but same as psql has its pgcli
mysql cli has its [mycli](https://www.mycli.net)
one of the great things it can do is to use mysql_config_editor encrypted credentials [https://www.mycli.net/loginpath]
1. Up/down arrows to scroll through command history
2. \timing will show the execution time of sql commands
3. \watch N allows you to repeat commands every N seconds, very handy in certain situations
Up/down errors: https://github.com/tomcam/postgres#scrolling-through-the-com...
Skeptically I tried, and after a little while, I prefer nothing else. I was already sold on vi, so it's not much surprise.
I use the \i command a lot, to run SQL commands from a file. That gives me a chance to nicely organize and indent complex queries, and tweak them as needed (with vi, of course).
That's also I do migrations. I script them in a long SQL file (surrounded by BEGIN and ROLLBACK). When it runs satisfactorily, I change ROLLBACK to COMMIT and run it again.
```
-- Extended display when it makes sense.
\x auto
-- Always time.
\timing
```
I also set `application_name` there. This lets others know I am connected.
I remember some university doing something similar as a workshop, but I can't recall which.
Over the course of classes, sometimes they do require you to use the command line but then they will just give you the commands to copy paste, without explaining what any of it does.
A bit over the top in some regards, but useful nonetheless.
The first is \e to open up your default $EDITOR for the query. In the past I've actually setup sublime text to open up my last run query, be able to edit it, close and save and it'll execute. I'd imagine you could do the same with vscode, though I'm currently just configured for vim personally.
The second isn't really psql commands, but that you can setup a psqlrc - https://www.craigkerstiens.com/2013/02/13/how-i-work-with-po...
\dt cust*
will list all tables that start with "cust". It works with other commands as well (like \l).
Also, I like to split my terminal using tmux, and have a neovim editor in one pane, psql in the other. I just keep doing "\i file_name.sql" as I perfect the query.
I definitely agree wholeheartedly about pspg. They even have a FoxPro theme, which has a special place in my heart :).
He evidently did some due diligence on who I was (AWS Tech Evangelist back then), and told me that he saw some of my emails in a PostgreSQL-related mailing list. I was so impressed! I told him I saw a bright future for PostgreSQL, especially after Sun's acquisition of MySQL (which I saw as a negative for the community).
Anyway, back on topic.
I keep thinking that PostgreSQL has a great opportunity ahead: in this decade, I would bet on the rise of an Oracle-like company (hopefully, less "evil" than Oracle), based on PostgreSQL. Possibly a company that will eventually go public.
Why? Because the tech is great, the community around it is great, and because we're in need of databases more than ever. (I could rant and get lost in a much longer explanation, but I want to spare you this time :D).
Do you see it this way? Any other viewpoints?
https://www.postgresql.org/docs/12/app-psql.html#APP-PSQL-ME...
psql postgresql://username:password@myhost.com:5432/databasename(Full disclosure: I added this. But I think it makes the output more readable. Just retry your examples and compare them.)