Common commands to help developers when dealing with databases, specifically postgresql.
An example of extracting data from the database, this sort of thing is useful if you are manually testing or checking what is in the database.
copy (select id from document) To '/tmp/docids.csv' with CSV DELIMITER ',' HEADER;
The bash command column
(see man column
) can be used to parse a csv file into JSON.
For example if a file test.csv
has the following contents:
1,one,uno
2,two,dos
3,three,tres
… is run through column
with the follow arguments…
column -J -s',' test.csv --table-columns=num,en,es
it will output:
{
"table": [
{
"num": "1",
"en": "one",
"es": "uno"
},{
"num": "2",
"en": "two",
"es": "dos"
},{
"num": "3",
"en": "three",
"es": "tres"
}
]
}
Restoring from a sql dump file, after running make start
docker-compose stop
psql
but this starts to get clumsy).
Note, we start by connecting to the postgres database so that we can drop and create the database in question $MYDB
.
export PGPASSWORD=password
export PGDATABASE=postgres
export MYDB=navigator
export PGHOST=localhost
export PGUSER=navigator
export PGPASSWORD=localpassword
psql -c "drop database $MYDB"
psql -c "create database $MYDB"
$MYDB
when connecting/restoring:
export PGDATABASE=$MYDB
psql -f mydump.sql
If you are doing this frequently you may wish to create your own script.