Category Archives: Postgresql

Export table structure

pg_dump -U username -d db_name -t table_name –exclude-table-data table_name > table_structure.sql

Advertisements

Remove \r \n from database data

test_db=# select address from test_table;

58/171-G, RAJAPURAM,\r                                                                                                                                                                                                                                                                                                                                                                                                              +
PALANIAPPA NAGAR,\r                                                                                                                                                                                                                                                                                                                                                                                                                 +
SALEM
TS.No.5823,SANTHANATHAPURAM FOURTH STREET\r                                                                                                                                                                                                                                                                                                                                                                                         +

 

solution
test_db=# update test_table set address= regexp_replace(address, E'[\\n\\r]+’, ‘ ‘, ‘g’);

test_db=# select address from test_table;

58/171-G, RAJAPURAM,

PALANIAPPA NAGAR,

SALEM TS.No.5823,SANTHANATHAPURAM FOURTH STREET

When was the database created in Postgres

postgres=#SELECT datname, (pg_stat_file(‘base/’||oid||’/PG_VERSION’)).modification AS datcreated
FROM pg_database;

datname | datcreated
——————-+—————————
template1 | 2014-05-06 15:44:37+05:30
template0 | 2014-05-06 15:44:38+05:30
postgres | 2014-05-06 15:44:38+05:30
abc | 2014-07-01 12:58:31+05:30
xyz | 2014-07-03 11:01:38+05:30

Mysql VS Postgres query

MySql                                              Postgres

show database                                  \l

show tables;                                       \d

desc table table_name;                    \d+  table_name    or   \d  table_name

Postgresql installation and create a user

Installing postgressql

$ sudo apt-get install postgresql

Installing pgadmin3

$ sudo apt-get install pgadmin3

To set password to the user postgres

$ sudo -u postgres psql postgres

postgres=# \password postgres

Create new user

$ sudo -u postgres createuser –superuser $USER

Set the password to the created user

$ sudo -u postgres psql

postgres=# \password $USER

Create database and go inside the database

$ createdb test

$ psql test

test=#