Quick intro to Postgres for developers

postgresIn this article, a quick guide to the most commonly used Postgres commands

INSTALL

sudo apt-get install postgresql postgresql-contrib

ADMIN OPERATIONS

Create superuser from the commad line

sudo -u postgres createuser –superuser digideps

Create database from command line

sudo -u postgres createdb newDatabaseName

Login via command line (postgres is the default root user)

sudo -u postgres psql [dbname]

DEVELOPER OPERATIONS / QUERIES

Describe table1

d+ table1;

Change password of the postgres user

password postgres

Grant permission on db1 to user1

grant all privileges on database db1 to user1

List databases

l

List users

du

List tables in a db

dt

IMPORT/EXPORT

Export db snapshot

sudo -u postgres pg_dump dbname —clean > dump.sql

Import db from snapshot

sudo -u postgres psql dbname < dump.sql