Start Postgres
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Stop Postgres
$ pg_ctl -D /usr/local/var/postgres stop -s -m fast
Create a Database
$ createdb mydb
Remove a Database
$ dropdb mydb
To start working with a database and use the Postgres terminal
$ psql mydb
Login as "postgres" (SuperUser) to start using database: # su - postgres
Create a new database: $ createdb mydb
Drop database: $ dropdb mydb
Access database: $ psql mydb
Get help: mydb=# \h
Quit: mydb=# \q
Read command from file: mydb=# \i input.sql
To dump a database: $ pg_dump mydb > db.out
To reload the database: $ psql -d database -f db.out
Dump all database: # su - postgres # pg_dumpall > /var/lib/pgsql/backups/dumpall.sql
Restore database: # su - postgres # psql -f /var/lib/pgsql/backups/dumpall.sql mydb
Show databases: #psql -l or mydb=# \l;
Show users: mydb=# SELECT * FROM "pg_user";
Show tables: mydb=# SELECT * FROM "pg_tables";
Set password: mydb=# UPDATE pg_shadow SET passwd = 'new_password' where usename = 'username';
Clean all databases (Should be done via a daily cron): $ vacuumdb --quiet --all