PostgreSQL on the Command Line
phili.pe
phili.pe
I found that I often want to do basic things, like see the schema and some sample values, maybe even the distribution of values.
So, I ended up building a web UI, to do all of these in just a few clicks. It's also a good way for non-developers to run basic queries - https://getvql.com
That hasn't been my experience with MySQL and other SQL-flavors, nor with the NoSQL projects like MongoDB and CouchDB.
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
For server side help, type 'help contents'Weird I know, but it works. Still psql is a superior client IMHO.
MySQL also has \c which I find tremendously useful, when you screw something up mid-query.
http://www.craigkerstiens.com/2013/02/21/more-out-of-psql/
http://www.craigkerstiens.com/2013/02/13/How-I-Work-With-Pos...
- it can't list schemas with \dn
- it can't list materialized views with \dm
- \dt <star>.users doesn't work as it does in psql, i.e. it just lists all tables due to the * instead of listing users tables in all schemas
- it can't use your editor with \e
- it can't input existing files with \i
- it can't redirect the output with \o
- it can't extract data with \copy
- \dn works
- \dm does not
- \dt <star>.users now appears to work as per psql
- \e works
- \i works
- \o does not
- \copy does not
@fpilipe, you mention you use one schema per customer. I'm curious as to how many people do this and how well it works for them. Any thoughts?
Pros:
- When you enter a psql session, you `SET search_path TO customer;` and that's it. No customer_id column in any table you have to remember setting in your WHERE clause.
- Same thing for rails console. At the beginning of the session you choose which customer you want to operate on.
- Similar for web requests. You just activate the right customer (e.g. based on subdomain) and that's it. Instead of `current_customer.posts.find(params[:id])` you can now simply do `Post.find(params[:id])`.
- Data is completely separated. It's almost impossible to have a bug causing data to spill from one customer to the other, which is quite high chance when you always have to remember to load data through the current customer.
Cons:
- Number of objects (tables, indexes, sequences) is multiplied by number of customers.
- Adding a new customer requires `pg_dump` to be present as it dumps the public schema (the blueprint), creates a new schema, and loads the dump into that schema.
- Migrations take longer as there are more objects to touch.
In our case, the pros outweigh the cons. One further advantage of the separate schemas is that, in theory, we could at any time start using completely separate databases, one for each customer or just one additional for a much larger customer that's eating up all resources on the DB.
I found this: http://stackoverflow.com/questions/2024884/commandline-overw... but it didn't seem to help.
export PAGER='less -S'
And the lines will not wrap in the terminal, when you have a lot of output.on the bash command line before starting psql should fix this. This was one of those things I figured out several years after I started using bash that would have made my life much less annoying.
It starts with logging into the DBMS. Importing/exporting/dumping databases from/to a file? Running queries from the shell? User management? Backups?
How to backup a Postgres DB?
mysqldump -u user -p DB > dump.sql
Import a DB from the Linux command line? mysql -u user -p DB < dump.sql
How to run a command directly from a shell script? mysql -u [user] -p[pass] -e "[mysql commands]"
When managing systems, this stuff is more important to me than the actual Postgres syntax.In short:
pg_dump -Fc mydb > db.dump
Restore: http://www.postgresql.org/docs/current/static/app-pgrestore....In short:
pg_restore -C -d postgres db.dump
Command directly from the shell: psql -c "psql command"pg_dump -C mydb | psql -h ${some_other_server} mydb
Import with csvsql http://csvkit.readthedocs.org/en/0.3.0/scripts/csvsql.html and export with sql2csv http://csvkit.readthedocs.org/en/latest/scripts/sql2csv.html