PGSQL Common Commands

This is my short list of the psql commands I need to use. PgSql is something of black magic for me at this time, and I do not have time enough to go into its nitty gritties so far. So, short steps to log in , drop database , create database etc. I am more used to MySQL and these commands are weird for me.

1. To login :

sudo -u postgres psql

If you get an error like:
psql: FATAL:  role "vibhu" does not exist

It means you are not using the correct psql user. For some reason, here the user is a user named postgres and that has administrative rights on the table.

2. To see the databases:
postgres=# \list

Self explanatory. Instead of show databases; just \list

3. To drop a database
postgres=# drop database somedatabase;

Do not forget the ; at the end. In example above 'somedatabase' is the name of the database.

4. To create a database
postgres=# create database somedatabase with owner vibhu;

Where 'somedatabase' is your database name, and 'vibhu' is the person who has ownership over the database. This I do as I do not want the admin user to be always working on the database.

5. Bonus : To reset the db on heroku ( I am using django on heroku )

heroku pg:reset DATABASE

No comments:

Post a Comment