This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
phpPgAdmin is a php-based web application that provides a GUI interface for the postgresql system. It performs a similar function to phpMyAdmin, which allows users to manipulate database information in a visual program in MySQL.
Start off by ensuring that the apt-get repository is up to date:
sudo apt-get update
Once the process has completed, go ahead and install postgresql, helpful additional dependencies, and phpgadmin. During its installation, phpPgAdmin will also install the required php and apache packages.
sudo apt-get install postgresql postgresql-contrib phppgadmin
Start Apache:
sudo service apache2 start
Once phpgadmin Is installed, you may be able to access it by going to youripaddress/phpPgAdmin.
You can run the following command to reveal your server’s IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'
You may find, however, that attempting to reach the phpPgAdmin page may result in a forbidden 403 error.
In order to make this page accessible, we should make it available to all visitors (don’t worry, we’ll lock it down in the next step):
sudo nano /etc/apache2/conf.d/phppgadmin
Within the file find the following section and uncomment the line, “allow from all”. The section should look like this:
order deny,allow deny from all allow from 127.0.0.0/255.0.0.0 ::1/128 allow from all
With the .htaccess file allowed, we can proceed to set up a native user whose login would be required to even access the phpPgAdmin login page.
Start by creating the .htaccess authentication section your site's configuration file. For example's sake, I will use the default site:
sudo nano /etc/apache2/sites-enabled/000-default
Follow up by setting up the user authorization. Create a new section within the virtual host file, pasting the following information in:
<Directory "/usr/share/phpPgAdmin"> AuthUserFile /etc/phpPgAdmin/.htpasswd AuthName "Restricted Area" AuthType Basic require valid-user </Directory>
Below you’ll see a quick explanation of each line
Now we will go ahead and create the valid user information.
Start by creating a htpasswd file. Use the htpasswd command, and place the file in a directory of your choice as long as it is not accessible from a browser. Although you can name the password file whatever you prefer, the convention is to name it .htpasswd.
sudo htpasswd -c /etc/phpPgAdmin/.htpasswd username
A prompt will ask you to provide and confirm your password.
Once the username and passwords pair are saved you can see that the password is encrypted in the file.
FInish up by restarting apache:
sudo service apache2 restart
phpPgAdmin will now be much more secure since only authorized users will be able to reach the login page. Accessing youripaddress/phpPgAdmin should display a screen like
Fill it in with the username and password that you generated. After you login you can access phpPgAdmin with your Postgres username and password.
Change the authentication method in the Authentication Configuration File:
sudo nano /etc/postgresql/9.1/main/pg_hba.conf
The change can be made in the following line:
# "local" is for Unix domain socket connections only local all all md5
To begin creating users, first switch into the default superuser and create the database that the user will log into:
sudo su - postgres
Subsequently, create a new database where you will store your tables:
createdb newdb
Although the database has been created, the only user with access to it is the default postgres user. We can allow other users to access and manipulate this database by creating new users.
Once logged in as the default superuser, you can move forward to create more roles in your PostgreSQL system.
To outfit your user with a password, you can add the option -P to the createuser command:
createuser -P
Enter name of role to add: newuser Enter password for new role: Enter it again: Shall the new role be a superuser? (y/n) y Password: enter the superuser’s password here
You can then log into postgres in one of several ways.
If you are logging in using the peer identification, you can simply type the following command, specifying the database that you are logging into:
psql newdb
If you are logging into using the md5 identification, you can include the user that you prefer to log in as:
psql –U newuser -W newdb
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi, is it necessary to run “sudo /etc/init.d/postgresql restart” after modifying pg_hba.conf?
@hsiaoshunwen you can reload the config file in a couple ways:
Restart db server: “sudo service postgresql restart”
Use pg_ctl: “sudo -u postgres pg_ctl reload [-D DATADIR]”, reload the config files without a full restart.
Use pg_ctlcluster: “sudo -u postgres pg_ctlcluster [version] [cluster] [action]” where action is reload. pg_lsclusters will give you those three params.
When running the line sudo htpasswd -c /etc/phpPgAdmin/.htpasswd username it is saying htpasswd: cannot create file /etc/phpPgAdmin/.htpasswd
Any ideas what im doing wrong?
Kind regards, Simon
Im getting this error message when attempting to visit the site The requested URL /phpPgAdmin was not found on this server. Any ideas?
For Ubuntu 14.10 edit /etc/apache2/apache2.conf and add the following line to it:
Include /etc/apache2/conf.d/phppgadmin
For older Ububtu releases only: to set up under Apache all you need to do is include the following line in /etc/apache2/apache2.conf.
Include /etc/phppgadmin/apache.conf
@swhitehouse do you have permission to create the file? Make sure you’re running the command through sudo. Did you install the phppgadmin package and restart apache?
This is for ‘swhitehouse’ and others:
Should read:
After setting up a password for the ‘phppgadmin’ page and the you go to ‘<your IP>/phppgadmin’ and its not bringing up the login dialog then change:
to:
Hi,
I`m on Accessing phpPgAdmin step,
but /etc/postgresql/9.1/main/pg_hba.conf dont exists
Someone can help me.
Thanks
Works for me, reinstalling the postgres
@hugoamorimlyra Glad you got it working! :]
If you’re getting “psql: FATAL: Peer authentication failed for user …” on the last steps where you’re trying out your new psql username/password then try restarting Postgresql the same way hsiaoshunwen suggested.
I am not getting the prompt as shown in the section Accessing phpPgAdmin. Any advice?
I am not getting the prompt as shown in the section: Accessing phpPgAdmin. Everything else went without error msg. Last terminal output is:
postgres@Lexington:~$ psql newdb psql (9.1.9) Type “help” for help.
newdb=# psql -U newuser -W newdb newdb-#
by the bye: this is the ONLY tutorial on installing Postgre that has ever ever worked this far. (I’m a retired guy and not the most 'puter literate. Any help appreciated. Thanks.
@dkearney, @neil.niekerk: Did you follow the “Configure the .htaccess Authentication” and the “Create the htpasswd file” steps?
Try restarting apache2 so the configuration changes take effect.
Kamal, All fixed, thank you. As noted above, the command: sudo htpasswd -c /etc/phpPgAdmin/.htpasswd username does not work. It had to be changed to: sudo htpasswd -c /etc/phppgadmin/.htpasswd username
@dkearney: Great, glad to hear you figured it out! :] Just out of curiosity - what Ubuntu version do you have on your droplet?
Where the word “username” appears in this line:
sudo htpasswd -c /etc/phpPgAdmin/.htpasswd username
what is to go where “username” is. I tried “Restricted Area” with and without quotes. I tried my user name for this computer, which is “mark” with and without quotes. Nothing worked. Also, where the line says: " Use the htpasswd command, and place the file in a directory of your choice as long as it is not accessible from a browser." Where is the file? What is it’s name? Could I change read permissions and keep it in /home? I’m uncertain how to make this file inaccessable to the browser.
@neil.niekerk: The file is stored in /etc/phpPgAdmin (/etc/phpPgAdmin/.htpasswd)
Replace username with the username you want to login as, it can be your first name. Apache blocks access to .ht* files by default so you don’t have to move it to your home directory.
Please, help-me, he show the error!!!
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
I can’t start PostgreSql.
I installed following the tutorial, but I can’t start the Postgres, anybody can help-me?
@willardy: Try running “service postgresql restart”
Nothing, no message!!!
@willardy: Did you install the postgresql package?
I installer the command:
sudo apt-get install postgresql postgresql-contrib phppgadmin
Ubuntu 12.04 LTS
@Kamal can you help-me?
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
@willardy what command did you run that output that error?
When I try to run psql I get a locale error. It is OK for psql but I cannot import dump files because of this. Any ideas: postgres@assay:/home/deployer$ psql perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = “en_US:en”, LC_ALL = (unset), LC_CTYPE = “UTF-8”, LANG = “en_US.UTF-8” are supported and installed on your system. perl: warning: Falling back to the standard locale (“C”). psql (9.1.9)
@sfcarroll2: Run the following commands and try again:
locale-gen en_US en_US.UTF-8 dpkg-reconfigure locales
Thank you Kamal!!! That worked.
Kamal - FYI : http://dba.stackexchange.com/questions/46525/postgres-locale-error/46526#46526
@sfcarroll2: Awesome! Glad I’ve been able to help! :]
I have installed redmine via https://www.digitalocean.com/community/articles/one-click-install-redmine-on-ubuntu-12-04-with-digitalocean and now have a problem installing postgresql. Just can’t access to IP/phpPgAdmin, could you help me with this?
@Leo: If your domain is working properly, you can edit /etc/apache2/sites-enabled/yourdomain and comment out the virtualhost that contains “ServerName ip.address”. Restart apache and I believe you will be able to access phpPgAdmin then.
Hello Kamal,
Now AuthUserFile /etc/phppgadmin/.htpasswd does not work, anybody can access http://redmine.yeleleo.co.uk/phppgadmin/, Apache restart does\t help.
@Leo: Do you still have that set up or did you destroy the droplet?
when i try psql incommand line i have an error psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
when i try service postgresql restart
No PostgreSQL clusters exist; see “man pg_createcluster”
@eav: Run “sudo pg_createcluster 9.2 main --start” and then try starting the postgresql service. Does that work?
hey guys to prevent all above error and one headache just run the following line before install postgresql:
export LC_ALL=“en_US.UTF-8”
I have a quite unusual nuisance going on, I am trying to export a db thru phppgadmin in SQL format, the error message i keep getting is that pg_dump file can be not executed since its “PATH” needs to be fixed in config.inc.php. I have done N number of combinations by now and still it won’t work, any clues, I have even tried chown www-data:www-data on pg_dump in correct path, and even that is not working, it seems to me like some bug with phppgadmin, is that so ?, any ideas ?
@niranjan81: What is <code>PATH</code> set to in <code>config.inc.php</code>?
the path is correctly set (i checked and tried /usr/bin/pg_dump as well as /usr/lib/postgresql/9.1/bin/pg_dump), I found it had something to do with php not allowed to run exec function and being run in safe_mode, but my phpinfo() says its not running in safe mode. I got it settled by running my local copy of pg_dump :-& and would definitely like to know if its possible to do so in phppgadmin without compromising on security :-).
Hi Setup all thing but i am not getting login dailog box. when i tried to hit the URL http://192.168.1.89:8080/phppgadmin a file got download which have page content.i think php page was not executing.
We had to update to php 5.5 and in the process updated Apache to 2.4.9. In the latest version of Apache the conf files need to be in conf-enabled.
See the answer here:
http://askubuntu.com/a/452110 <pre> sudo cp /etc/apache2/conf.d/phppgadmin /etc/apache2/conf-enabled/phppgadmin.conf sudo service apache2 restart </pre> Also, as a side note, your virtual hosts (in sites-enabled) should end in .conf, too.
Many people have commented that sudo htpasswd -c /etc/phpPgAdmin/.htpasswd username does not work.
For later versions of postgres the directory is /etc/phppgadmin/ (note the lowercase) So substitute “phpPgAdmin” with lower case letters “phppgadmin” wherever you find it in the tutorial and everything should be OK.
This comment has been deleted
Hi, i’m at “Accessing phpPgAdmin” step and i’m getting the 404 error when attempting to visit the site at <myIP>/phppgadmin.
I put the “Directory” tag into the “/etc/apache2/sites-enabled/000-default.conf” file, instead of “/etc/apache2/sites-enabled/000-default” because “000-default” file does not exist. Is it correct?
Also, I put the “Directory” tag after de “VirtualHost” tag. Is it correct too?
I’m using ‘phppgadmin’ instead of ‘phpPgAdmin’ in all cases.
Any help?
This step seems to be missing from the instructions…
For Ubuntu 14.10 edit /etc/apache2/apache2.conf and add the following line to it:
Include /etc/apache2/conf.d/phppgadmin
For older Ububtu releases only: to set up under Apache all you need to do is include the following line in /etc/apache2/apache2.conf.
Include /etc/phppgadmin/apache.conf
When running this line:
"sudo htpasswd -c /etc/phppgadmin/./htpasswd username’
I get the following error message:
“sudo: htpasswd: command not found”.
Any fix to this? I put the changes of <Directory> in a file within /etc/apache2/sites-enabled/000-default.conf rather than just 000-default, as it didn’t exist
In the section Configure the .htaccess Authentication
has to be