Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to setup postgres initdb with a custom data directory?

Asked by Andrea Bailey Oct 6, 2022 9.4K views 3 answers
Share

About this question

I am trying to set up Postgres 12 on Centos 8. Postgres shall be installed in the default directory i.e. /var/lib/pgsql, however I want the data directory to be in /data/pgsql I want to use postgresql-setup as root, as I believe it will create systemd service files along with it, rather than using pg_ctl or running initdb as postgres user.


However, if I try

$ postgresql-setup --initdb --pgdata=/data/pgsql/

I will receive the following error.


postgresql-setup: unrecognised option '--pgdata=/data/pgsql'

FATAL: can't parse arguments

What is the best way to achieve this?

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 21, 2025

To set up PostgreSQL's initdb with a custom data directory, follow these steps:

1. Choose a Custom Data Directory

PostgreSQL stores database files in a default location, but you can specify a custom one.

For example, let's set it to:

  /custom/path/to/pgdata

2. Run initdb with the -D Option

Use the -D flag to specify the custom data directory:

  initdb -D /custom/path/to/pgdata

This initializes the PostgreSQL cluster in the specified folder.

3. Set Permissions for the Directory

Ensure PostgreSQL has the correct permissions:

  sudo mkdir -p /custom/path/to/pgdatasudo chown -R postgres:postgres /custom/path/to/pgdatachmod 700 /custom/path/to/pgdata

4. Configure postgresql.conf to Use the Custom Directory

Modify PostgreSQL’s configuration file to reflect the new data directory:

  sudo nano /etc/postgresql//main/postgresql.conf

Update the data_directory setting:

  data_directory = '/custom/path/to/pgdata'

5. Start PostgreSQL with the New Data Directory

If PostgreSQL is running as a service:

  sudo systemctl restart postgresql

If manually starting the server:

  pg_ctl -D /custom/path/to/pgdata start

6. Verify the Custom Data Directory

Check if PostgreSQL is using the correct directory:

  SHOW data_directory;
Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 24, 2024

To set up PostgreSQL initdb with a custom data directory, you'll need to follow these steps:

Choose or Create a Custom Data Directory:

Decide on the location where you want to store your PostgreSQL data. Choose a directory with appropriate permissions. For example:

mkdir /path/to/custom/data/directory

Initialize the Database Cluster:

Use the initdb command to initialize a new PostgreSQL database cluster with your custom data directory. Specify the location of your custom data directory using the -D or --pgdata option. For example:

initdb -D /path/to/custom/data/directory

Customize Initialization Parameters (Optional):

You can customize the initialization parameters by modifying the PostgreSQL configuration files located in your data directory (postgresql.conf, pg_hba.conf, etc.) before starting the server. This step is optional but allows you to tailor the server configuration to your specific needs.

Start the PostgreSQL Server:

After initializing the database cluster, you can start the PostgreSQL server. Use the pg_ctl command with the -D option to specify the location of your custom data directory. For example:

  pg_ctl -D /path/to/custom/data/directory start

Connect to the Database:

Once the server is running, you can connect to the PostgreSQL database using a PostgreSQL client tool like psql. Specify the database name, username, and host as needed. 

For example:

  psql -d mydatabase -U myusername -h localhost

Verify Installation:

After connecting to the database, you can verify that everything is set up correctly by running SQL queries or commands like l to list databases, dt to list tables, etc.

Make sure to replace /path/to/custom/data/directory with the actual path to your custom data directory. By following these steps, you can set up PostgreSQL initdb with a custom data directory and start using your database with the desired configuration.



Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.