Monday, March 16, 2015

PostgreSQL install and creating DB in ubuntu


https://help.ubuntu.com/community/PostgreSQL

Installation

To install use the command line and type:
 sudo apt-get install postgresql postgresql-contrib
This will install the latest version available in your Ubuntu release and the commonly used add-ons for it.
See "External Links" below for options for getting newer releases.

Installing PostGIS, procedural languages, client interfaces, etc

Additional packages contain procedural language runtimes, add-ons like PostGIS, language client interfaces like psycopg2 for Python, etc. You can get a listing with:
 apt-cache search postgres

Administration

pgAdmin III is a handy GUI for PostgreSQL, it is essential to beginners. To install it, type at the command line:
 sudo apt-get install pgadmin3
You may also use the Synaptic package manager from the System>Administration menu to install these packages.

Basic Server Setup

To start off, we need to change the PostgreSQL postgres user password; we will not be able to access the server otherwise. As the “postgres” Linux user, we will execute the psql command.
In a terminal, type:
sudo -u postgres psql postgres
Set a password for the "postgres" database role using the command:
\password postgres
and give your password when prompted. The password text will be hidden from the console for security purposes.
Type Control+D to exit the posgreSQL prompt.

Create database

To create the first database, which we will call "mydb", simply type:
sudo -u postgres createdb mydb

Using pgAdmin III GUI

To get an idea of what PostgreSQL can do, you may start by firing up a graphical client. In a terminal type :
 pgadmin3
You will be presented with the pgAdmin III interface. Click on the "Add a connection to a server" button (top left). In the new dialog, enter the address 127.0.0.1 (Local host is default, so it can be left out.), a description of the server, the default database ("mydb" in the example above), your username ("postgres") and your password. One more step is required in order to allow pgAdmin III to connect to the server, and that is to edit pg_hba.conf file and change the authentication method from peer to md5 (Will not work if you have not set the password.):
sudo nano /etc/postgresql/9.3/main/pg_hba.conf
and change the line 
# Database administrative login by Unix domain socket
local   all             all                                peer
to
# Database administrative login by Unix domain socket
local   all             all                     md5

***************OR
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all  127.0.0.1/32  md5
# IPv6 local connections:
host all all ::1/128 md5
*****************

Now you should reload the server configuration changes and connect pgAdmin III to your PostgreSQL database server.
sudo /etc/init.d/postgresql reload
With this GUI you may start creating and managing databases, query the database, execute SQl etc.
----------------------------------------------------------------------

Check whether Postgres is running or not !


ubuntu@ip-172-31-48-240:~$ service postgresql status
9.3/main (port 5432): online

Restarting PostgreSQL Server

To start PostgreSQL server, run:

sudo -u postgres /etc/init.d/postgresql (or /etc/init.d/postgresql8) stop/start

No comments:

Post a Comment