Academind Logo
Postgres: Start, Stop, Uninstall & Reset Password

Postgres: Start, Stop, Uninstall & Reset Password

Learn how you may stop or start or uninstall your Postgresql server - and how you may reset the root user password. For macOS & Windows!

Created by Maximilian Schwarzmüller

As a developer, when working with Postgresql, you typically focus on database commands and queries like INSERT INTO, SELECT etc.

But some basic database administration knowledge also comes in handy from time to time.

This article explains how to perform some of the most important, common tasks:

#

Stopping a running server (Windows)

If you installed Postgresql via the official installer, you can start and stop it as a Windows service via the "Services" tool (a default Windows tool). Look for a service with a name that starts with "postgresql" and simply stop or start this service as needed.

You can then also restart the server if you want to.

#

Stopping a running server (macOS)

You can stop the running Postgresql server via a command called pg_ctl which was installed together with Postgresql. You find the command executable inside the /Library/PostgreSQL/<version>/bin folder. You can either add that folder to your global PATH variable or you navigate into that folder first before you try to execute the pg_ctl command.

cd /Library/PostgreSQL/<version>/bin

Once you have access to the pg_ctl command, you can stop the Postgresql server by running

pg_ctl stop

If that fails, you can try running

pg_ctl stop -D /Library/PostgreSQL/<version>/data

If that doesn't work (e.g. because of permission errors), you could also try this alternative approach:

sudo lsof -i:5432

This should output a list of processes listening on port 5432 - which is the default Postgresql server port. If you chose a different default port during installation, you of course should use that port number instead of 5432.

With that list, you can run this command to end the Postgresql server process:

sudo kill -15 <process id>

The <process id> can be found in the PID column of the output.

If you're still getting more errors, this thread might be worth a closer look.

#

Stopping a running server (Linux)

You can stop a running Postgresql server via

sudo service postgresql stop

That's all! You can thereafter restart the server if you want to.

#

Starting a server (Windows)

If you installed Postgresql via the official installer, you can start and stop it as a Windows service via the "Services" tool (a default Windows tool). Look for a service with a name that starts with "postgresql" and simply stop or start this service as needed.

#

Starting a server (macOS)

You can start the Postgresql server via a command called pg_ctl which was installed together with Postgresql. You find the command executable inside the /Library/PostgreSQL/<version>/bin folder. You can either add that folder to your global PATH variable or you navigate into that folder first before you try to execute the pg_ctl command.

cd /Library/PostgreSQL/<version>/bin

Once you have access to the pg_ctl command, you can start the Postgresql server by running

pg_ctl start -D /Library/PostgreSQL/<version>/data

The argument after -D (/Library/PostgreSQL/<version>/data in this case) is the path to the "data" directory. That's the directory where the actual database data will be written to. You can choose any path you want but the path shown here is the default path used by Postgresql.

#

Starting a server (Linux)

You can start a running Postgresql server via

sudo service postgresql stop
#

Uninstalling Postgresql (Windows)

You can uninstall Postgresql via the "Control Panel" => "Programs and Features". Search for Postgresql and simply choose "Uninstall". In the wizard, which opens up, you can then choose if you want to uninstall everything or only selected parts.

You might also want to manually remove any "data" folders you assigned to different databases whilst you were using Postgres.

#

Uninstalling Postgresql (macOS)

Postgresql comes with an uninstaller which you can (and should) invoke if you want to get rid of it. You find that uninstall-postgres tool in the installation folder, by default /Library/PostgreSQL/<version>.

Execute the uninstall-postgres.app tool which you find there, to remove Postgresql from your system. You can either remove the entire application or selected parts.

You might also want to manually remove any "data" folders you assigned to different databases whilst you were using Postgres.

#

Uninstalling Postgresql (Linux)

The guide explains in detail, how you may remove Postgres on Linux systems.

#

Resetting the root user password

In case you forgot your root user password, you can reset it by temporarily disabling password checks and then changing the password to a new one manually.

To disable password checks, you need to edit the pg_hba.conf file which can be found in the "data" folder of your database server. By default, that folder is located in the Postgresql installation directory (C:\Program Files\PostgreSQL\<version>\ on Windows, /Library/PostgreSQL/<version>/ on macOS).

To access the "data" folder on macOS, you first need to run

sudo su postgres

This ensures that you do get access to that folder. Note: You only get access in the terminal where you ran this. Hence you should cd into the data folder and then open the pg_hba.conf file with an editor that's built into the terminal. For example by running nano pg_hba.conf.

On Windows, you should be able to navigate into the "data" folder and open the pg_hba.conf just like that (with a text editor).

In the opened pg_hba.conf file, find the local all all <encryptionmethod> entry and replace it with local all all trust.

On macOS, using nano, you can navigate through the opened file via the arrow keys and then edit the appropriate line. Once you're done, you can save the changes by pressing CTRL + O, then exit the file via CTRL + X.

Once the file was changed, you should be able to connect to the Postgresql server (via any client) without entering a password.

After connecting, you can now update the root user by running

ALTER USER postgres WITH ENCRYPTED PASSWORD 'new-password';

After executing this command, you can close the connection and turn password authentication back on by editing the pg_hba.conf file again (as described above).

Find the local all all trust you edited before and change it back to local all all <encryptionmethod> (encryptionmethod is the encryption method that was specified in the file before).