启动 postgresql 服务并指定数据目录

Hom*_*lli 5 postgresql ubuntu

我最近使用 apt-get install 在 Ubuntu 10.0.4 上安装了 pg 8.4。

它将 pg 安装为一项服务,该服务在机器启动时启动。但是,我已经在我希望 postgres 使用的特定位置创建了一个数据库集群。

这是我如何创建集群的命令行输出:

username@jupiter:~$ sudo /usr/lib/postgresql/8.4/bin/initdb --encoding=UTF8 --pgdata=/mydata/pgdbdata/
initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
username@jupiter:~$ /usr/lib/postgresql/8.4/bin/initdb --encoding=UTF8 --pgdata=/mydata/pgdbdata/
The files belonging to this database system will be owned by user "username".
This user must also own the server process.

The database cluster will be initialized with locale en_US.utf8.
The default text search configuration will be set to "english".

fixing permissions on existing directory /mydata/pgdbdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 24MB
creating configuration files ... ok
creating template1 database in /mydata/pgdbdata/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    /usr/lib/postgresql/8.4/bin/postgres -D /mydata/pgdbdata
or
    /usr/lib/postgresql/8.4/bin/pg_ctl -D /mydata/pgdbdata -l logfile start

username@jupiter:~$ 
Run Code Online (Sandbox Code Playgroud)

现在,我尝试启动 postgresql 服务:

这是命令行输出:

username@jupiter:~$ /usr/lib/postgresql/8.4/bin/pg_ctl start -D /mydata/pgdbdata/
server starting
username@jupiter:~$ FATAL:  could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": Permission denied
Run Code Online (Sandbox Code Playgroud)

所以我有两个问题:

  1. 如何启动服务并指定数据位置(我做错了什么?)

  2. 我希望在机器启动/重新启动时启动服务,以使用新的数据目录。我需要编辑哪个文件以确保我始终使用正确的数据文件夹获得服务?

swa*_*eck 7

有错误提示...问题是您将 initdb 作为本地登录名运行,但我很高兴您的登录名 ( username) 无权访问/var/run/postgresql. 如果您有全新安装,我可以建议您删除数据目录并重新开始,这次是通过 su-ing 进入 postgres 帐户:

username@jupiter:~$ sudo su - postgres
Run Code Online (Sandbox Code Playgroud)

然后你可以 initdb

postgres@jupiter:~$ /usr/lib/postgresql/8.4/bin/initdb --encoding=UTF8 --pgdata=/mydata/pgdbdata/
Run Code Online (Sandbox Code Playgroud)

此外,正如 Catcall 所提到的,请确保您使用的数据位置正确。