6

In my fresh Ubuntu 18.04 --

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:    18.04
Codename:   bionic

Installed PostgreSQL 10 --

$ apt list --installed | grep -i postgres
postgresql-client-10/bionic,now 10.3-1 amd64 [installed]
postgresql-client-common/bionic,bionic,now 190 all [installed,automatic]
postgresql-common/bionic,bionic,now 190 all [installed,automatic]
postgresql-server-dev-10/bionic,now 10.3-1 amd64 [installed]
postgresql-server-dev-all/bionic,bionic,now 190 all [installed]

Both service and systemctl return immediately with no message but didn't start any postgres process --

$ sudo service postgresql restart
$ sudo systemctl restart postgresql

I'm completely at dark as there's neither conf nor log --

$ ll /etc/postgresql
total 8
drwxr-xr-x  2 root root 4096 Feb  8 12:26 ./
drwxr-xr-x 95 root root 4096 May  6 04:11 ../

$ ll /var/log/postgresql/
total 8
drwxrwxr-t  2 root postgres 4096 Feb  8 12:26 ./
drwxrwxr-x 10 root syslog   4096 May  6 04:10 ../

What is going on there? Any pointer will be much appreciated.

Edit: to make things worse, PostgreSQL 10 is the only listed package in Ubuntu 18.04, there's no obvious "apt" way to install older version of PostgreSQL

2
  • 1
    $ sudo systemctl status postgresql ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since Sun 2018-05-06 06:19:47 UTC; 2h 13min ago Process: 15349 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 15349 (code=exited, status=0/SUCCESS) May 06 06:19:47 myapp systemd[1]: Starting PostgreSQL RDBMS... May 06 06:19:47 myapp systemd[1]: Started PostgreSQL RDBMS. – Jerry Ji May 6 '18 at 8:34
  • 1
    Who/Why downvoted this question? – Jerry Ji May 6 '18 at 8:38
4

From the result of apt list --installed you don't have a PostgreSQL 10 server actually installed.

The name of the package is exactly postgresql-10 : https://packages.ubuntu.com/bionic/database/postgresql-10 . You should install that package.

The lack of error of systemctl and the fact that a postgresql service does exist are confusing: it's because postgresql is an "umbrella" service that launches every postgresql instance installed and configured. In your case you currently have zero such instance, but that's fine as far as the postgresql service is concerned. In the most general case, you may have several different versions of PostgreSQL running concurrently (from different packages postgresql-<version>), and several instances of the same version too (from the same package).

I'd suggest to check your PostgreSQL instances with pg_lsclusters rather than systemctl. See also pg_ctlcluster to control them.

4

Just use

service postgresql@10-main start

and it works. Don't know exactly why this happens in 18.04 only (I verified 17.10 and it doesn't happen there), but the 'umbrella' seems unable to enumerate the installed instances.

HTH,

1
2

05/13/2018

DISCLAIMER: This is for postgresql 10 and ubuntu 18.04, and may or may not work for other versions. PS: If you have been tampering with the language settings lately, please tell me, because there may be a connection with the postgres failure

Yesterday I was having the exact same problem, and nobody on the entire internet could help me, so I went rogue... And it worked!

First and foremost, if you have any database with data you are interested in keeping, I cant help you there; you will have to figure out a way to backup all your data.

Now onto the actual steps (this is exactly what I did, the steps between [ ] you may skip):

sudo apt remove --purge postgres*
[ sudo apt remove --purge pg* ]
sudo apt autoremove
sudo apt autoclean
sudo apt clean
[ sudo find / -name "*postgres*" -type f -delete ] -> this may delete any backups

Now make sure you have this line in /etc/apt/sources.list

deb http://cz.archive.ubuntu.com/ubuntu bionic main

If you don't, just add it... Lets continue:

sudo apt update
sudo apt upgrade
sudo apt install -y postgresql-10 postgresql-contrib postgresql-client
[ sudo apt install -y postgresql-server pgadmin3 ] -> this you may need for metasploit
[ sudo reboot ]

Now you have to check if the necessary directories were created:
/etc/postgresql/10/main -> config files
/usr/lib/postgresql/10/bin -> scripts & executables
/var/run/postgresql -> temporary files
/var/lib/postgresql/10/main -> folders

If any of those don't exist, I cant help you. You also need to make sure the user postgres exists Lets continue:

sudo chown root /usr/lib/postgresql -R
sudo chgrp root /usr/lib/postgresql -R
sudo chmod 755 /usr/lib/postgresql -R

Now go edit /etc/environment and add this to the PATH: /usr/lib/postgresql/10/bin

sudo chown postgres /etc/postgresql -R
sudo chgrp postgres /etc/postgresql -R
sudo chmod 700 /etc/postgresql -R

sudo echo "" > /etc/postgresql/10/main/postgresql.log
sudo chown postgresql /etc/postgresql/10/main/postgresql.log
sudo chgrp postgresql /etc/postgresql/10/main/postgresql.log
sudo chmod 700 /etc/postgresql/10/main/postgresql.log

sudo mkdir /var/run/postgresql/10-main.pg_stat_tmp
sudo chown postgres /var/run/postgresql/10-main.pg_stat_tmp -R
sudo chgrp postgres /var/run/postgresql/10-main.pg_stat_tmp -R
sudo chmod 700 /var/run/postgresql/10-main.pg_stat_tmp -R


Create /var/lib/postgresql/.bashrc and write this to it

shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=2000
shopt -s checkwinsize
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    color_prompt=yes
    else
    color_prompt=
    fi
fi 
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi
source /etc/environment

And then:

sudo chown postgresql /var/lib/postgresql/.bashrc
sudo chgrp postgresql /var/lib/postgresql/.bashrc
sudo chmod 664 /var/lib/postgresql/.bashrc
[ sudo reboot ]


And now for the final part:

sudo su
su postgres

To start postgresql:

pg_ctl start -D /etc/postgresql/10/main -l /etc/postgresql/10/main/postgresql.log

To end it:

kill $(cat /var/run/postgresql/10-main.pid)

Basic configuration (enter the postgresql interpreter):

psql

\du+ -> list postgresql users
\l -> list postgresql databases
createuser and dropuser -> self explanatory
createdb and dropdb -> self explanatory


EXTRA: METASPLOIT

If anyone reading this needs postgresql for metasploit, you can follow this link (its for 16.04 but works fine in 18.04), there is a mistake however: At some point, you will need to run this:
rvm --default use ruby-${RUBY_VERSION}@metasploit-framework gem pristine --all Instead of the suggested:
rvm --default use ruby-${RUByVERSION}@metasploit-framework

Furthermore, if you run into trouble with the ruby bundle install part, this command may save your life: gem pristine --all

4
  • sudo apt remove --purge pg* – Jerry Ji May 14 '18 at 10:06
  • did you try it? – Mr.Robot May 14 '18 at 12:29
  • Yes, and that's how I caught your typo. BTW, thanks for your detailed steps, I wish I had enough reputation to up vote your answer – Jerry Ji May 15 '18 at 13:04
  • FYI to verify the installation using psql: sudo -u postgres psql postgres – Daniel Jun 9 '18 at 6:50
1

Had the same issue here. Removed everything postgres related (apt remove --purge), deleted all remaining directories from postgres 9 and reinstalled postgres 10 again. Then config files under /etc/postgresql/ appeared and database could be started. HTH.

1

DISCLAIMER: This is for postgresql 10 and ubuntu 18.04, and may or may not work for other versions.

Follow the answer provided by Mr Robot except recognize:

  • The user and group are postgres, so the commands should use chown -R postgres:postgres ...
  • I'm not sure what's up with /lib/systemd/system/postgresql.service although somehow /lib/systemd/system/postgresql@.service manages a PostgreSQL cluster? Either way I guess it was already running by the time I followed the instructions, and after reboot (which should be unnecessary?) it came back online, so I guess it works.
0

Now you have to check if the necessary directories were created: ... If any of those don't exist, I cant help you.

I can help. In my case many (mostly synaptic) purges and re-installs did not work, the config directories remained empty. The answer was to do the purge and reinstall on the command line:

sudo apt-get purge postgresql* && apt-get install postgresql postgresql-client

This dumped out lots of log messages, including importantly:

...
Setting up postgresql-10 (10.8-0ubuntu0.18.04.1) ...
Error: The locale requested by the environment is invalid:
  LANG: en_GB.utf8
  ...
Error: could not create default cluster. Please create it manually with

     pg_createcluster 10 main --start

or a similar command (see 'man pg_createcluster').

So the explanation is that my locale settings were not as preferred by the default installation routine, but only by running the purge and install in the terminal did this become apparent.

Running the specified pg_createcluster command worked, I now have config files and can connect to the server with psql as the postgres user.

0

Just in case... Ubuntu 18.4 with 2 instances of postgresql installed (8.4 and 10). With

service postgresql start

I can start both instances but it doesn't give any message, so after starting service I have to run

ps -ef | grep postgres

to make sure both of them are running:

postgres 27019     1  0 15:46 ?        00:00:00 /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf
postgres 27024 27019  0 15:46 ?        00:00:00 postgres: 10/main: checkpointer process   
postgres 27025 27019  0 15:46 ?        00:00:00 postgres: 10/main: writer process   
postgres 27026 27019  0 15:46 ?        00:00:00 postgres: 10/main: wal writer process   
postgres 27027 27019  0 15:46 ?        00:00:00 postgres: 10/main: autovacuum launcher process   
postgres 27028 27019  0 15:46 ?        00:00:00 postgres: 10/main: stats collector process   
postgres 27029 27019  0 15:46 ?        00:00:00 postgres: 10/main: bgworker: logical replication launcher   
postgres 27041     1 12 15:46 ?        00:00:00 /usr/lib/postgresql/8.4/bin/postgres -D /var/lib/postgresql/8.4/main -c config_file=/etc/postgresql/8.4/main/postgresql.conf
postgres 27046 27041  0 15:46 ?        00:00:00 postgres: writer process   
postgres 27047 27041  0 15:46 ?        00:00:00 postgres: wal writer process   
postgres 27048 27041  0 15:46 ?        00:00:00 postgres: autovacuum launcher process   
postgres 27049 27041  0 15:46 ?        00:00:00 postgres: stats collector process   

Under CentOS you have pgstartup.log log with error messages when your instance can't be load (there's one log file for each postgres installation), but I couldn't find anything like this in Ubuntu. After some searches without success I read something that led me to general log in /var/log/syslog log file, and there I could find what I was looking for:

Oct 22 15:26:18 my-machine systemd[1]: Starting PostgreSQL Cluster 10-main...
Oct 22 15:26:19 my-machine postgresql@10-main[26620]: Use of uninitialized value $data_directory in concatenation (.) or string at /usr/share/perl5/PgCommon.pm line 211.
Oct 22 15:26:19 my-machine postgresql@10-main[26620]: message repeated 2 times: [ Use of uninitialized value $data_directory in concatenation (.) or string at /usr/share/perl5/PgCommon.pm line 211.]
Oct 22 15:26:19 my-machine postgresql@10-main[26620]: Error: Invalid data directory for cluster 10 main
Oct 22 15:26:19 my-machine systemd[1]: postgresql@10-main.service: Can't open PID file /run/postgresql/10-main.pid (yet?) after start: No such file or directory
Oct 22 15:26:19 my-machine systemd[1]: postgresql@10-main.service: Failed with result 'protocol'.
Oct 22 15:26:19 my-machine systemd[1]: Failed to start PostgreSQL Cluster 10-main.

Note: In my case error messages were due to some tests I was making with different options in postgresql.conf file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.