Category: Postgresql


Export table structure

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

Backuping a single table from a database

pg_dump -U username -d database1 -t table_name > /home/backups/table_name.sql

Restoring the table into another database

psql -U username -d database2 < /home/backups/table_name.sql

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

Grant Privileges Postgres

Database:

GRANT ALL PRIVILEGES ON DATABASE DATABASENAME to USER;

Table:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO USER;

Id Seq Tables:

GRANT ALL PRIVILEGES  ON ALL SEQUENCES IN SCHEMA public TO USER;

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                                              Postgres

show database                                  \l

show tables;                                       \d

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

If you want to search column name as last_modified you will give the following query in postgres

select table_name from information_schema.columns where column_name = 
'last_modified

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=#