Showing posts with label PostgreSQL. Show all posts
Showing posts with label PostgreSQL. Show all posts

Saturday, July 30, 2016

How to Backup and Restore PostgreSQL 9.5.1 Database

Change your current user to postgres
root@ubuntu:~# su postgres
postgres@ubuntu:/root$ cd /tmp/

Listing all database inside postgresql
postgres@ubuntu:/tmp$ psql -l
List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
newdes    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres+
|          |          |             |             | =c/postgres
(4 rows)

Backing up single database (using pg_dump not pg_dumpall)


For your info, you can't restore plain backup using pg_restore, but you must using psql tool, for more information about this, you can read this.

Plain Backup (text file)
you can open this backup using text editor, like vi, nano, mcedit, pico, etc.
postgres@ubuntu:/tmp$ pg_dump newdes > /tmp/newdes_30072016.sql

Compressed binary format
postgres@ubuntu:/tmp$ pg_dump -Fc newdes > /tmp/newdes_30072016.bak

Tarball file
can restoring using pgadmin
postgres@ubuntu:/tmp$ pg_dump -Ft newdes > /tmp/newdes_30072016.tar

postgres@ubuntu:/tmp$ ls -al /tmp/
total 10592
drwxrwxrwt  3 www-data users      20480 Jul 30 10:08 .
drwxr-xr-x 22 root     root        4096 Jul 20 16:19 ..
-rwxrw-rw-  1 root     kraken   6059213 Jul 30 08:12 kraken.backup
drwx------  2 root     root        4096 Jul 30 09:56 mc-root
-rw-rw-r--  1 postgres postgres  465051 Jul 30 10:08 newdes_30072016.bak
-rw-rw-r--  1 postgres postgres 2112274 Jul 30 10:08 newdes_30072016.sql
-rw-rw-r--  1 postgres postgres 2174464 Jul 30 10:08 newdes_30072016.tar



Restoring backup database

postgres@ubuntu:/tmp$ psql --version
psql (PostgreSQL) 9.5.1

Create empty database
From root user (or another user)
root@ubuntu:~# createdb -h localhost -p 5432 -U postgres newdes
Password: ****

Or from postgres user
root@ubuntu:~# su postgres
postgres@ubuntu:/tmp$ psql -l
postgres@ubuntu:/tmp$ createdb newdes;

Listing all database inside postgresql
postgres@ubuntu:/tmp$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 newdes    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres+
           |          |          |             |             | =c/postgres
(4 rows)


Restoring Plain backup (database already exist)

Alternative command 1
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql newdes -f newdes_30072016.sql
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----

Alternative command 2
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql -f newdes_30072016.sql template1
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----

Alternative command 3
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql -U postgres < newdes_30072016.sql
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----

Alternative command 4
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql -f newdes_30072016.sql postgres
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----


Restoring compressed binary format
pg_restore -Fc newdes_30072016.bak

Restoring tarball file
pg_restore -Ft newdes_30072016.tar


Restoring Plain backup (database NOT exist)

Restoring compressed binary format
pg_restore -Fc -C newdes_30072016.bak

Restoring tarball file
pg_restore -Ft -C newdes_30072016.tar

How to Backup and Restore PostgreSQL 9.5.1 Database

Change your current user to postgres
root@ubuntu:~# su postgres
postgres@ubuntu:/root$ cd /tmp/

Listing all database inside postgresql
postgres@ubuntu:/tmp$ psql -l
List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
newdes    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
|          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres+
|          |          |             |             | =c/postgres
(4 rows)

Backing up single database (using pg_dump not pg_dumpall)


For your info, you can't restore plain backup using pg_restore, but you must using psql tool, for more information about this, you can read this.

Plain Backup (text file)
you can open this backup using text editor, like vi, nano, mcedit, pico, etc.
postgres@ubuntu:/tmp$ pg_dump newdes > /tmp/newdes_30072016.sql

Compressed binary format
postgres@ubuntu:/tmp$ pg_dump -Fc newdes > /tmp/newdes_30072016.bak

Tarball file
can restoring using pgadmin
postgres@ubuntu:/tmp$ pg_dump -Ft newdes > /tmp/newdes_30072016.tar

postgres@ubuntu:/tmp$ ls -al /tmp/
total 10592
drwxrwxrwt  3 www-data users      20480 Jul 30 10:08 .
drwxr-xr-x 22 root     root        4096 Jul 20 16:19 ..
-rwxrw-rw-  1 root     kraken   6059213 Jul 30 08:12 kraken.backup
drwx------  2 root     root        4096 Jul 30 09:56 mc-root
-rw-rw-r--  1 postgres postgres  465051 Jul 30 10:08 newdes_30072016.bak
-rw-rw-r--  1 postgres postgres 2112274 Jul 30 10:08 newdes_30072016.sql
-rw-rw-r--  1 postgres postgres 2174464 Jul 30 10:08 newdes_30072016.tar



Restoring backup database

postgres@ubuntu:/tmp$ psql --version
psql (PostgreSQL) 9.5.1

Create empty database
From root user (or another user)
root@ubuntu:~# createdb -h localhost -p 5432 -U postgres newdes
Password: ****

Or from postgres user
root@ubuntu:~# su postgres
postgres@ubuntu:/tmp$ psql -l
postgres@ubuntu:/tmp$ createdb newdes;

Listing all database inside postgresql
postgres@ubuntu:/tmp$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 newdes    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres+
           |          |          |             |             | =c/postgres
(4 rows)


Restoring Plain backup (empty database already exist)

Alternative command 1
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql newdes -f newdes_30072016.sql
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----

Alternative command 2
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql -f newdes_30072016.sql template1
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----

Alternative command 3
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql -U postgres < newdes_30072016.sql
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----

Alternative command 4
postgres@ubuntu:/tmp$ createdb newdes;
postgres@ubuntu:/tmp$ psql -f newdes_30072016.sql postgres
SET
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
CREATE SCHEMA
ALTER SCHEMA
----


Restoring compressed binary format
pg_restore -Fc newdes_30072016.bak

Restoring tarball file
pg_restore -Ft newdes_30072016.tar


Restoring Plain backup (database NOT exist)

Restoring compressed binary format
pg_restore -Fc -C newdes_30072016.bak

Restoring tarball file
pg_restore -Ft -C newdes_30072016.tar

Friday, October 19, 2012

[SOLVED] Getting PostgreSQL 9.1.4 module for PHP-5.4.7 on Slackware 14

Slackware 14 by default carrying PHP-5.4.7 and running smooth well, BUT unfortunately it doesn't come with PostgreSQL, after searching for a while, i decided to install PostgreSQl from scratch.

PostgreSQL 9.1.4 :
SlackBuild : postgresql.tar.gz

Follow this step to compile and install PostgreSQL 9.1.4 on Slackware 14.0 :
1. cd /tmp
2. wget ftp://ftp.postgresql.org/pub/source/v9.1.4/postgresql-9.1.4.tar.bz2
3. Extract SlackBuild script tar -zxvf postgresql-9.1.4.tar.gz
4. Go to Extracted directory cd postgresql-9.1.4
5. Run this command sh postgresql.SlackBuild /tmp/postgresql-9.1.4.tar.bz2
6. After all finish, it will create /tmp/postgresql-9.1.4-i486-1_SBo.tgz
7. Install it using installpkg postgresql-9.1.4-i486-1_SBo.tgz
8. Run this command su postgres -c "initdb -D /var/lib/pgsql/data"
9. Then run this /etc/rc.d/rc.postgresql start
10. Done

Now, the hard part ^^, actually after you find solution, you will said...Hhhmmm just like that hah :D, because PHP-5.4.7 inside Slackware 14 is not compiled using --with=pgsql option, and because i don't want to touch anything about PHP that i'm already set all option and some configuration running smothly on PHP-5.4.7, and also because my server is used for production. the point is, i just want to add pgsql.so into php.ini. That's it, nothing more.

And here is the solution.

PHP-5.4.7 :
SlackBuild : php-pgsql.tar.gz ( I Know this script for PHP-5.4.6, just relax okay ^^ )

Follow my step please :
1. cd /tmp
2. wget http://www.php.net/distributions/php-5.4.7.tar.bz2
3. wget http://slackbuilds.org/slackbuilds/14.0/libraries/php-pgsql.tar.gz
4. Extract SlackBuild script tar -zxvf php-pgsql.tar.gz
5. Go to extracted directory cd php-pgsql
6. Then mcedit php-pgsql.SlackBuild, and change version to correct one (5.4.7)
7. mcedit php-pgsql.info and change, change version and MD5SUM too.
8. Then run php-pgsql.SlackBuild /tmp/php-pgsql.tar.gz
9. After finish, you should get /tmp/php-pgsql-5.4.7-i486-1_SBo.tgz
10. Installpkg /tmp/php-pgsql-5.4.7-i486-1_SBo.tgz
11. Enable pgsql module by editing mcedit /etc/php/pgsql.ini and enable extension=pgsql.so by removing ;
12. Done

Proved :
root@slackware:~/Desktop# php -m | grep pgsql
If everything okay, you will get pgsql as result.

My info.php


Wednesday, December 14, 2011

PostgreSQL 9.0.4 with Slackware 13.37

PostgreSQL is an advanced object-relational database management system (ORDBMS) based on POSTGRES. With more than 15 years of development history, it is quickly becoming the de facto database for enterprise level open source solutions. This build includes full text search support (tsearch2).
Home: http://www.postgresql.org.Repository: SlackersDownload size: 4,26 MBInstalled size: 22,48 MBPackage filename: postgresql-9.0.4-i686-1cf.txz
Step 1: Download postgresql-9.0.4-i686-1cf.txz for Slackware 13.37 from here and install it.
#installpkg postgresql-9.0.4-i686-1cf.txz
Step 2: Create postgreSQL user account
# adduser postgres# passwd postgresChanging password for user postgres.New UNIX password:Retype new UNIX password:passwd: all authentication tokens updated successfully.

Configuring PostgreSQL 9.0.4 on Slackware 13.37

After succesfully install postgresql on slackware, the next point is to configuring postgresql so it can accessing from network ( local network).


Step 1: edit postgresql.conf
#mcedit /var/lib/pgsql/data/postgresql.conf
change at the connections and authentication section, find the
listen_addresses = 'localhost' and change it to
listen_addresses = '*'
and uncomment
#port =5432 so it becomes
port = 5432
and save it.

Step 2: edit pg_hba.conf
Because i connecting postgresql server from my laptop which have ip number 192.168.110.131 and some times i would like connected from another 192.168.110. ip number, so i editing pg_hba.conf like this
#mcedit /var/lib/pgsql/data/pg_hba.conf
addes this line to pg_hba.conf
host    all     all     192.168.110.0/24     md5

Getting PostgreSQL 9.0.4 module for PHP-5.3.8 on Slackware 13.37

I wanted to play around with PostgreSQL on the test server at work today, and found the slackpack over at LinuxPackages. I was very pleased that I wouldn’t have to compile PostgreSQL from source, as such complex apps will take more time than I would like to invest.

Anyway, once I’ve installed the package, and initialised the database, I decided to give phpPgAdmin a whirl on my freshly baked PostgreSQL installation, but when I try to use it I get an error message telling me that I have not compiled proper database support into my PHP installation.

I checked out my phpinfo(); and sure enough, Slackware‘s package was not built with the --with-pgsql option.

Most of my googling results mention that the only solution is to recompile PHP from scratch, including the --with-pgsql flag. Hmm, I'm really hate to replace my original PHP installation which was done in the “proper” way, ie. via the available slackpack. Who know what the hell I can possibly misconfigure and break!

Tuesday, December 13, 2011

PostgreSQL 9.0.4 with Slackware 13.37

PostgreSQL is an advanced object-relational database management system (ORDBMS) based on POSTGRES. With more than 15 years of development history, it is quickly becoming the de facto database for enterprise level open source solutions. This build includes full text search support (tsearch2).
Home: http://www.postgresql.org.
Repository: Slackers
Download size: 4,26 MB
Installed size: 22,48 MB
Package filename: postgresql-9.0.4-i686-1cf.txz
Step 1: Download postgresql-9.0.4-i686-1cf.txz for Slackware 13.37 from here and install it.
#installpkg postgresql-9.0.4-i686-1cf.txz
Step 2: Create postgreSQL user account
# adduser postgres# passwd postgresChanging password for user postgres.New UNIX password:Retype new UNIX password:passwd: all authentication tokens updated successfully.

Postgresql 8.4.7 on CentOS 6.0 (64 Bit)

Install Postgresql
# yum install postgresql-server postgresql-devel postgresql-contrib postgresql#
# chmod 777 -R /tmp <-- make sure you do this !!!!
# /etc/rc.d/init.d/postgresql initdb
Initializing database: [ OK ]
# /etc/init.d/postgresql start
Starting postgresql service: [ OK ]
# /etc/init.d/postgresql stop
Stopping postgresql service: [ OK ]
Edit postgresql configuration
# mcedit /var/lib/pgsql/data/postgresql.conf
# line 59: listen all
listen_addresses = '*'
# line 334: change log format
log_line_prefix = '%t %u %d '
port = 5432