PostgreSQL Installation in Linux(Python)
PostgreSQL is a powerful, open source object-relational database system. It runs on all major operating systems, including Linux, UNIX and Windows. It has native programming interfaces for C/C++, Java, .Net, Perl, Python, Ruby, Tcl, ODBC.
Install PostgreSQL in your Linux Machine
If you are using Ubuntu you can use the apt command
- apt-get install postgresql
and if you are using Pardus you can use pisi
- pisi it postgresql
Create a Database and grant permissions to Users
Add a Linux/UNIX user called odol
- adduser odol
- passwd odol
This’ll create the user odol. This is not a required step. This is only for testing purpose. You can use any existing user also.
Becoming a superuser and connect to database server
- su – postgres
This user will get created automatically when we install PostgreSQL.
- psql template1
If you are not becoming a superuser, you can do the following
- psql -d template1 -U postgres
Add a user called odol and Create a database called mydb and Grant Permissions
By the execution of last command, you’ll enter the postgres shell. As the db is template1, the prompt will be template=#
- CREATE USER odol WITH PASSWORD ‘myPassword’;
- CREATE DATABASE mydb;
- GRANT ALL PRIVILEGES ON DATABASE mydb to odol;
Enter as odol and test
- su – odol
- psql -d mydb -U odol
This will let you get into the shell for mydb database. In which you can create table and do other database activities and queries.
Using PostgreSQL with Django
In order to use Python with PostgreSQl we need an adapter. Psycopg is the most popular PostgreSQL adapter for the Python programming language.
Install the following requirements first
- apt-get install python-dev libpq-dev
We can get pyscopg using pip or easy_install
- pip install pyscopg2
Common Errors while installing pyscopg2
The most common error(the one I came across) while installing pyscopg2:
- Error: pg_config executable not found.
Make sure you have installed python-dev and libpq-dev in your system. The step is already mentioned above.