postgres usage

first things first

assuming postgres admin user not pgsql

default user @netbsd -- pgsql
default user @slackware -- postgres

get into the right prompt with –either–

sudo -u postgres psql

–or–

su - postgres -c psql

–or– yet another way

psql -U postgres

–or– even remotely

psql -h HOST -p PORT -U DBUSER
# -d DBNAME

database & user creation

create database "THAT-DB";

and create a user

create user THAT-USER with password 'PASSWORD-HERE';
grant all on database "that-db" to THAT-USER;
^D

change password

alter user THAT-USER password 'PASSWORD-HERE';

dba ops

list databases

\list

pick one

\c DBNAME

list tables

\dt

dba scripting

write output to file

\o out.txt
select * from TABLE;
\o
^D

as CSV

psql --csv ...

\o out.csv
select * from TABLE;
\o
^D

resources

https://stackoverflow.com/questions/12746854/postgresql-how-to-start-up-the-database-server-and-create-a-database

https://dba.stackexchange.com/questions/1285/how-do-i-list-all-databases-and-tables-using-psql

scripting

https://stackoverflow.com/questions/5331320/psql-save-results-of-command-to-a-file

https://stackoverflow.com/questions/6521531/how-do-you-print-the-result-of-a-postgresql-query-in-csv-or-tsv-format-from-the

HOME | GUIDES | PLAYBOOKS | LECTURES | LAB | CONTACT | HTML | CSS
Licensed as MIT