cheat-fork-echo/cheat/cheatsheets/psql

27 lines
847 B
Text
Raw Normal View History

2017-07-01 02:34:32 +02:00
# psql is the PostgreSQL terminal interface. The following commands were tested on version 9.5.
# Connection options:
# -U username (if not specified current OS user is used).
# -p port.
# -h server hostname/address.
2017-06-30 01:23:47 +02:00
2017-07-01 02:34:32 +02:00
# Connect to a specific database:
2017-06-30 01:23:47 +02:00
psql -U postgres -h serverAddress -d dbName
2017-07-01 02:34:32 +02:00
# Get databases on a server:
2017-06-30 01:23:47 +02:00
psql -U postgres -h serverAddress --list
2017-07-01 02:34:32 +02:00
# Execute sql query and save output to file:
psql -U postgres -d dbName -c 'select * from tableName;' -o fileName
2017-06-30 01:23:47 +02:00
2017-07-01 02:34:32 +02:00
# Execute query and get tabular html output:
psql -U postgres -d dbName -H -c 'select * from tableName;'
# Execute query and save resulting rows to csv file:
psql -U postgres -d dbName -t -A -P fieldsep=',' -c 'select * from tableName;' -o fileName.csv
# Read commands from file:
2017-06-30 01:23:47 +02:00
psql -f fileName
2017-07-01 02:34:32 +02:00
# Restore databases from file:
psql -f fileName.backup postgres