Creating postgresql Database and user in a nutshell.

By | 20th November 2014

I will not write much but just the code required to create a user and let the user access the db.

Create User
CREATE USER tom WITH PASSWORD 'myPassword';

Create a DB
CREATE DATABASE jerry;

Grant Privileges to user on the DB
GRANT ALL PRIVILEGES ON DATABASE jerry to tom;

Grant Privileges to table

GRANT ALL PRIVILEGES ON TABLE side_adzone TO tom;

Grant Privileges to user for copying files (especially import csv to DB)

GRANT pg_read_server_files TO tom;

Change ownership of the DB
ALTER DATABASE jerry OWNER TO tom;

Delete user
DROP USER tom;

Update user password

ALTER USER user_name WITH PASSWORD 'new_password';

Drop DB
DROP DATABASE jerry;

Backup and Restore databases

To take a backup use this command. The following command works only with postgres user so first change the user to postgres su postgres and then dump. make sure the directory is writable by postgres user.
pg_dump db_name > db_name.sql

To restore run the command as postgres user
psql db_name < db_name.sql