How I work with Postgres – psql, My PostgreSQL Admin
craigkerstiens.com
craigkerstiens.com
Tweaking and playing with gnuplot is a loss of time - if on a copy/paste excel and others can understand the data from the label and plot using reasonable defaults without many hints, certainly if columns are identified as datetime, labels etc. there could be a tool to use such hints and make a decent graph (to me, decent means giving a global understanding - sure you can tweak it to look good if you are preparing a report, but a lot of time is spent graphing thinks to figure things out and many graphs go to the trash in the process)
My dream is to do my select queries in psql and direct the output to that tool, never leaving psql - so it could be for example something that would be triggered on a new table creation matching a specific name like xx_, then it would simply require prefixing "select" by "create table xx_abc as ".
The best way I've found is to save the output to a CSV and pass it to other tools, but there are never quite user friendly and usually can't pick reasonable defaults.
There is an OSX psql frontend I tried after it was recommended here on HN (http://inductionapp.com/) but it was not that helpful in day to day operations.
Yet it seemed to be on the same problem - see this picture https://s3.amazonaws.com/induction/induction-visualize.png
# psql -U postgres
psql (8.4.15)
Type "help" for help.
postgres=# \t
Showing only tuples.
postgres=# \a
Output format is unaligned.
postgres=# \f ' '
Field separator is " ".
postgres=# select * from example;
1 1
2 2
3 3
4 4
postgres=# \o | /usr/bin/gnuplot
postgres=# select 'set title "My Graph"; set terminal dumb 78 24; set key off; set ylabel "Time"; set xlabel "Servers";' || 'plot ''-'' with lines;' ; select * from example;
postgres=# \o
My Graph
Time
4 ++----------+----------+-----------+----------+-----------+---------**
+ + + + + + **** +
| **** |
3.5 ++ **** ++
| **** |
| **** |
3 ++ *** ++
| **** |
| **** |
2.5 ++ **** ++
| **** |
| **** |
2 ++ *** ++
| **** |
| **** |
1.5 ++ **** ++
| **** |
+ **** + + + + + +
1 **----------+----------+-----------+----------+-----------+---------++
1 1.5 2 2.5 3 3.5 4
Servers
postgres=#I imagine you could put it all into a user-defined postgresql function, so all you have to say is:
graph(select * from example); # \set foo '(select now())'
# :foo;
?column?
--------
2013-02-14 04:18:23+01
# select count(*) from :foo as foo;
count
-----
Afaik macros just expand inline, do you can embed stuff like \o.Ian Barwick writes how to put all the prep stuff into a psql script, then all you have to do is define your query and invoke the script:
-- start quote --
What you could do is create a small psql script along these lines:
barwick@localhost:~$ cat tmp/plot.psql
\set QUIET yes
\t\a\f ' '
\unset QUIET
\o | /usr/bin/gnuplot
select 'set title "My Graph"; set terminal dumb 78 24; set key off; set ylabel "Time"; set xlabel "Servers";' || 'plot ''-'' with lines;' ;
:plot_query;
\set QUIET yes
\t\a\f
\unset QUIET
\o
barwick@localhost:~$ psql -U postgres testdb
psql (9.2.3)
Type "help" for help.
testdb=# \set plot_query 'SELECT * FROM plot'
testdb=# \i tmp/plot.psql
My Graph
4 ++---------+-----------+----------+----------+-----------+---------**
+ + + + + + **** +
| **** |
3.5 ++ **** ++
| **** |
| **** |
3 ++ **** ++
| **** |
2.5 ++ ***** ++
| **** |
| **** |
2 ++ **** ++
| **** |
| **** |
1.5 ++ **** ++
| **** |
+ **** + + + + + +
1 **---------+-----------+----------+----------+-----------+---------++
1 1.5 2 2.5 3 3.5 4
Servers
testdb=#
-- end quote --And
Sergey Konoplev explains how to do it with a server-side function - you need gnuplot installed on the db server -
-- start quote --
plpython/plperl/etc plus this way of calling
select just_for_fun_graph('select ... from ...', 'My Graph', 78, 24, ...)
will do the trick.
-- end quote --Heres a little more on it:
http://theexceptioncatcher.com/blog/2012/11/really-cool-thin...
Come on, tell the truth: how many of you have had The Great and Powerful Oz for a DBA? (there's a wonderful BOFH episode about the BOFH vs a DBA - read when in a foul mood some time for a chuckle)
Fortunately, most all the DBAs I've worked with do in fact value data integrity, but, don't seem to care about other development practices like automated DB version migrations or reducing repetition.
All the different ways Navicat packages its products are unnecessarily confusing. Premium Essentials ($20) is almost certainly what you want: http://www.navicat.com/en/products/navicat_essentials/essent...
And by accident or design, there is almost no interference between the psql commands and the editor.
Not that the other tools have set a very high bar. pgAdmin crashes constantly, and you can't sort columns by clicking on them. Navicat doesn't run properly on Linux, you have to mess around with wine. phpPgAdmin has a single DB host is hard coded into a config file and just feels antiquated.
One major drawback of TeamPostgreSQL so far: it doesn't support SSL connections.
Postgresql 9 Administration Cookbook, Simon Riggs, Hannu Krosing
See http://stackoverflow.com/questions/14598261/making-sublime-t...
We're making a lot of progress with it, including a lower price offering and launching a free tier soon. Check it out, or shoot me an email if you're interested.
setting PROMPT2 to null string gifts you with ability to easily copy-paste SQL expressions
`-n` disables readline lib because we don't need it in Acme
Having 2 windows open is about the same as having 2 panels in one of the DB "Monties". http://catb.org/jargon/html/M/monty.html
Here's my ~/bin/sqlplus shell script pointing to the Oracle instant client install:
#!/bin/sh
OIC_DIR=$HOME/opt/oracle-instant-client
export LD_LIBRARY_PATH="${OIC_DIR}:$LD_LIBRARY_PATH"
export TNS_ADMIN="${OIC_DIR}"
rlwrap "${OIC_DIR}/sqlplus" "$@"
Bonus: You can use a tnsnames.ora file with the instant client if you put it in the instant client directory and export the environment variable in the script above.Thanks for the tip on rlwrap, in case I have to use that thing again. I'm assuming it provides GNU readline support on top of the base tool.
I've found it to be quite good, you can express powerful stuff with the keyboard directly, once you know what you're doing. A GUI is always less versatile in forming joins and search criteria. Once you start having a dropdown query builder, what's the point?
I do use a separate editor often, nano or sublime, depending if I'm working on the remote or local end. Both have syntax highlight, and then it's the natural path to scripts and version control from that.
(1) pgAdminIII's query tool (not the tedious query builder, yes the bare SQL ("pencil" button) query tool, a plain text editor and SQL runtime
(2) An editor able to both allow open files to be changed externally, and notify me that that's happened (no file close/reopen; can leave one file open for many round trips here). Also, have it make whitespace characters visible so one can see TAB characters. EditPlus does these job for me.
(3) A spreadsheet program open to a blank, unnamed, unsaved sheet. You guessed it, I'm talking Excel.
Here's the workflow:
(A) Run the next try of the query-build-in-progress in the query tool, sending its output to the file "simultaneously open" on EditPlus. Include headers, and use a column separator that's unlikely to occur in data, e.g. the pipe symbol, '|'. Almost all keystrokes.
(B) Switch to EditPlus. It then politely notices the file's been changed, assent to its doing a file reload (no close/re-open in the UI, but of course that's what it does). Two keystrokes.
(C) Globally change | to \t, but don't bother to save the file. Just select all and copy to clipboard. A few keystrokes.
(D) Switch to spreadsheet, paste. Two keystrokes. Here is where using the TAB character as a column separator works its good magic; all the headings and all the query result drop into properly aligned cells in the spreadsheet.
Analyze the results in the spreadsheet, maybe highlight some color on problematic rows, columns or cells. Go back to the SQL editor and re-run the whole chain.
No file naming (except once on startup), no import wizard (ugh!), no open/close, no CSV misinterpretation. All stock software.
Various ways of icing this particular workflow-cake:
* After pasting into the sheet, highlight the columns that Excel typed as numeric when you know they're character values that happen to be all digits. Use 'Format Cells' to change the 'Number' (data type!) from 'General' to 'Text'. Nothing will appear to change, but here's the magic: Highlight all the cells and delete their content (delete key). Now paste from clipboard again. This type the character data hasn't lost its leading zeroes, and it's properly left-justified.
* Start with a SELECT ... then once the result is in Excel, delete the colums it turns out you don't want... then hightlight those headings, copy to clipboard, paste to editor, reformat as comma-separated, copy and paste that in place of the in the original select.
* Use underscores in column names instead of camel-case, e.g. row_id instead of rowId (camel-case doesn't work in PG unless column identifiers are quoted, not worth the pain). Once in Excel, highlight all the column names, repalce underscore with blank, set the Format to Wrap. Now the column names form a distinctive taller-than-the-data-rows, easy-to-read.
I've come to think of (and use) the clipboard as a manual, stepwise imitation of the character stream native to shell scripting of *nix CLI tools. Not a true 'toolchain' in that one has to manually pump everything through the clipboard, but but still effective and quick enough for fast, "filenameless" turnaround on query development, and very little mousework.
Most of all, it gives me the very significant power of a spreadsheet for query error analysis.
Does anyone know of a tool that has a good visual DB design interface? That is what I am missing (not just when I work with postgresql, but period). I want to be able to easily and visually see the relationships between tables, not just the tables themselves, or one table with a list of its relationships. Something like this: http://ondras.zarovi.cz/sql/demo/ but not web based and actually supporting all of postgresql?
We then export it into MySQL database, generate a migration based on that, and then apply the migration to a Postgres database. Before we used migrations, we instead had a shell script with a series of sed commands to map the MySQL SQL output to SQL that will work in Postgres.
So, there are options, but I'd very much like a visual DB designer for Postgres as well.
http://wiki.postgresql.org/wiki/GUI_Database_Design_Tools
http://wiki.postgresql.org/wiki/Community_Guide_to_PostgreSQ...
It's got some of the best table visualisation and table/tool/translation/QC intergration visual design tools I've seen.
It's essentially built with the goal of starting with multiple table sources in various ASCII / <some>SQLDB format and displaying all tables, building filter pipes to merge and translate data on the fly and produce single or multiple coherent databases and table sets (or even more ascii tables) as output.
It has it's quirks but it's a solid bit of kit ( I used it some years back to read in and unify data from several million leases (geospatial boundaries and related metadata) from multiple sources (Australian, Canadian, South African, etc. land departments) - from whoa to go was about four days, once running it chewed through the data on par with normal copy speeds (eg: it imported cleaned & filtered data in a time ballpark to just copying the data from A->B).
I'm not affiliated, but on the basis of that job, yeah, I'll spruik it.
[2] http://www.safe.com/fme/fme-technology/fme-desktop/overview/
( See desktop demo video from overview section )