PGSQL CheatSheet Mysql2psql
PGSQL CheatSheet Mysql2psql
PGSQL CheatSheet Mysql2psql
html
Users toggling between MySQL and Postgres are often confused by the equivalent commands to
accomplish basic tasks. Here's a chart listing some of the differences between the command line client
for MySQL (simply called mysql), and the command line client for Postgres (called psql).
MySQL (using mysql)
\c Clears the buffer
\d string Changes the delimiter
\e Edit the buffer with external
editor
\g Send current query to the
server
\h Gives help - general or
specific
\e (same)
Notes
Postgres also allows \e filename which
will become the new buffer
\g (same)
\h (same)
The pager is only used when needed
based on number of rows; to force it
on, use \pset pager always
No equivalent
No equivalent
\c dbname (same)
No equivalent
\x (same)
\! command (same)
\dt (same)
No equivalent
Updates: Added PSQL_PAGER and \o |tee filename, thanks to the Davids in the comments section.
Added \t back in, per Joe's comment.
A note on the "show create table" equivalent, you can make use of \! to run system commands, so -->
\! pg_dump --schema-only -t tablename
will show you the create sql without having to leave psql.
MySQL command
SHOW DATABASES;
PostgreSQL equivalent
\l[ist]
USE some_database;
\c some_database
SHOW TABLES;
\dt
DESCRIBE some_table;
\d+ some_table
\di
Postgres
mysqldump
pg_dumpall
Description
pg_dumpall is designed to dump all of the databases and calls pg_dump
to do it. It can also be used to dump global values like roles and
tablespaces.Example with mysql:
mysqldump all-databases > /path/to/file.sql
Example with postgres:
pg_dumpall > /path/to/file.sql
pg_dump is used for dumping individual databases.Example with mysql:
mysqldump mydatabase > /path/to/file.sql
mysqldump
n/a
pg_dump
pg_restore
innodb_top
pg_top
mysql
psql
innodb_top does not ship with mysql and is a third party executable. It
shows you things like inserts per second, updates per second, transactions
per second and gives a good overview as to what is going on with the
serverpg_top shows similar things but is laid out more similarly to the
native linux top program.
This is the command to enter the CLI utility. You can also pass sql into
the utility from the linux command line like shown below.Passing query
with mysql (-e for execute):
mysql -e select 1;
Passing query with postgres (-c for command):
psql -c select 1;
Exit
Mysql{quit|
exit|[ctrl-c]}
Exit
Postgres\q
I completely felt like a noob the first time I got into the psql CLI. I tried
entering all of the things I would typically enter to exit mysql, or even
terminals and other programs to no avail. I had to google search it and
discovered that postgres does just about everything with slash commands.
select now()
pg_last_xact
_replay_time
stamp() AS
replication_d
elay;
\G
\x
stop slave;
start slave;
select
pg_xlog_repl
ay_pause();
select
pg_xlog_repl
ay_resume();
slave. If the master hasnt written anything, the slave has not applied
anything and can show you that it is behind. It is recommended to use a
written timestamp from cron every minute to gauge replication. This
achieves 2 things, it will guarantee regular writes to the master which will
replicate to the slave, and monitoring can look to the timestamp in a
particular location to know if the server is behind. This is similar to the
heartbeat checks that percona recommends in mysql.
Sometimes it is nice to have information displayed in a non-row format.
Mysql achieves this with using a \G at the end of the query and it will
output each row of database data is a key-value pair. Postgres calls this
expanded output mode. You can toggle expanded output mode to on by
typing \x[enter] and then running your query normally. This is a session
setting, so if you want to go back to row format, you can toggle it back to
off with the same action.