PostgreSQL(Postgres) is a very popular open source Database Management System. I use it frequently in my Django projects. Here are few tips for setting up postgres from the very beginning. I will also discuss about troubleshooting some of common problems during it’s usage.
Installing Postgres
Installing postgres is very straightforward.
sudo apt-get -y install postgresql postgresql-contrib
If you are using postgres in a django project, you might like to install some dependencies for postgres to work with django.
sudo apt-get install -y libpq-dev python-dev
Configure Posgres
After installing postgres, you can use it to manage your databases. You can start by creating a database and creating a user to manage the database. To create a database, first of all switch to user postgres as
sudo su – postgres
Then, create your database using the following command
createdb testdatabase
Replace “testdatabase” with any database name you wish to create. Then, create a user using the command
createuser -P testuser
Replace “testuser” with any username you like. You should be prompted to enter a password for the new user. Enter a strong password.
Now, we want the newly created user to be able to manage the newly created database. So, to grant the user permission for the new database, first of all, start the PostgreSQL interactive terminal as
psql
Then, use the command below to grant the user “testuser” permissions to use database “testdatabase”
GRANT ALL PRIVILEGES ON DATABASE testdatabase TO testuser;
That’s all. Use \q to exit the postgres terminal and then enter exit to exit the postgres user mode.