Getting started with Postgresql. Centos Redhat Fedora

How to install PostgreSQL on Centos / Redhat / Fedora.

Hi All, In this article I want to quickly show you specific steps to install PostgreSQL 8 database on CENTOS Linux 5. As we all know that we can use yum to install packages / softwares, I am using yum for this installation. To make sure that you have everything needed do:

#yum list | grep postgresql

Then verify that you see:

postgresql.i386, postgresql-server.i386 and postgres-libs.i386. if you have 64 bit version then you will see x86_64.

Centos installation comes with postgresql-lib installed. If it is not installed then please follow:

#yum install postgresql-libs

Now, the general installation. As root install postgres core and postgresql server:

#yum install postgresql postgresql-server

We need a seperate user as owner of postgresql database owner so create postgres user:

#adduser postgres

A directory to store datafiles caleed the ‘datafile’ for the database:

#mkdir -p /usr/local/pgsql/data

It is necessary to change ownership of the data files to the postgres user:

#chown postgres /usr/local/pgsql/data

now change your role to postgres user from root user:

#su – postgres

Initialize the datafiles for the database:

#/usr/bin/initdb -D /usr/local/pgsql/data

In most of the cases it will starStart the database with initialized datafiles as the background process (&) and log all messages and errors (2&1) in the logfile:

#/usr/bin/postgres -D /usr/local/pgsql/data > logfile 2>&1 &

Create the test database:

#/usr/bin/createdb firstdb

Log in to the test database:

$/usr/bin/psql firstdb
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

firstdb=>

Setup PostgreSQL database (RDBMS) on Centos-Redhat-Fedora.
Install and configure PostgreSQL database (RDBMS) using rpm. (redhat packages)
One of the best open source RDBMS system – PostgreSQL (pgsql)
Powerful yet free RDBMS system with active development – PostgreSQL.

PostgreSQL Cheat Sheet

CREATE DATABASE

CREATE DATABASE dbName;

CREATE TABLE (with auto numbering integer id)

CREATE TABLE tableName (
 id serial PRIMARY KEY,
 name varchar(50) UNIQUE NOT NULL,
 dateCreated timestamp DEFAULT current_timestamp
);

Add a primary key

ALTER TABLE tableName ADD PRIMARY KEY (id);

Create an INDEX

CREATE UNIQUE INDEX indexName ON tableName (columnNames);

Backup a database (command line)

pg_dump dbName > dbName.sql

Backup all databases (command line)

pg_dumpall > pgbackup.sql

Run a SQL script (command line)

psql -f script.sql databaseName

Search using a regular expression

SELECT column FROM table WHERE column ~ 'foo.*';

The first N records

SELECT columns FROM table LIMIT 10;

Pagination

SELECT cols FROM table LIMIT 10 OFFSET 30;

Prepared Statements

PREPARE preparedInsert (int, varchar) AS
  INSERT INTO tableName (intColumn, charColumn) VALUES ($1, $2);
EXECUTE preparedInsert (1,'a');
EXECUTE preparedInsert (2,'b');
DEALLOCATE preparedInsert;

Create a Function

CREATE OR REPLACE FUNCTION month (timestamp) RETURNS integer
 AS 'SELECT date_part(''month'', $1)::integer;'
LANGUAGE 'sql';

Table Maintenance

VACUUM ANALYZE table;

Reindex a database, table or index

REINDEX DATABASE dbName;

Show query plan

EXPLAIN SELECT * FROM table;

Import from a file

COPY destTable FROM '/tmp/somefile';

Show all runtime parameters

SHOW ALL;

Grant all permissions to a user

GRANT ALL PRIVILEGES ON table TO username;

Perform a transaction

BEGIN TRANSACTION
 UPDATE accounts SET balance += 50 WHERE id = 1;
COMMIT;

Basic SQL

Get all columns and rows from a table

SELECT * FROM table;

Add a new row

INSERT INTO table (column1,column2)
VALUES (1, 'one');

Update a row

UPDATE table SET foo = 'bar' WHERE id = 1;

Delete a row

DELETE FROM table WHERE id = 1;

http://www.mydailylinux.com/my-daily-linux/node/pgsql-cheat-sheet

Postgresql: show databases, show tables, show columns

Hi all, In this article you will know some very very basic commands about postgresql. When i started learning postgresql, I found bit strange when “show databases”, “show tables”, and “show columns” command did not work. I was using mysql and I am still using mysql but it is just matter of learning one more database as postgresql started becomming popular now. So.. If you have question in your mind how to list databases, tables and colums in postgresql database then I have answer for you.

First of all to connect to postgresql you need to use :
psql -d “database name” -h “hostname OR IP” -U “user name” command. This will prompt you to enter password.
Note: By default you can not connect to postgresql with user name root.

  1. To list databases aaccessible to user you connected with
    mysql: SHOW DATABASES
    postgresql: \l
    postgresql: SELECT datname FROM pg_database;
  2. To list tables in your database 
    mysql: SHOW TABLES
    postgresql: \d
    postgresql: SELECT table_name FROM information_schema.tables WHERE table_schema = ‘public’;
  3. To list columns in particular table / schema use : 
    mysql: SHOW COLUMNS
    postgresql: \d table
    postgresql: SELECT column_name FROM information_schema.columns WHERE table_name =’table‘;
  • Login to post comments