简体   繁体   English

创建没有root权限的PostgreSQL数据库

[英]Create PostgreSQL Database without root privilege

Currently, I use 目前,我用

$ sudo service postgresql start    

to start the PostgreSQL server and 启动PostgreSQL服务器和

$ sudo -u postgres createdb testdb --owner ownername

to create a database. 创建数据库。 However, these commands need root privilege. 但是,这些命令需要root权限。 How can I do these without root privilege/sudo on Linux (Ubuntu)? 如何在Linux(Ubuntu)上没有root权限/ sudo的情况下执行这些操作?

You can run PostgreSQL without root privs by creating a new instance (which PostgreSQL calls a "cluster") and starting it. 你可以通过创建一个新实例(PostgreSQL称之为“集群”)并启动它来运行没有root权限的PostgreSQL。

You can't use the Ubuntu init scripts, wrapper tools like pg_ctlcluster , etc if you do this. 如果这样做,您不能使用Ubuntu init脚本, pg_ctlcluster等包装工具。 You must use only PostgreSQL's own tools. 您必须只使用PostgreSQL自己的工具。

To create the new PostgreSQL instance with the superuser equal to your username, data directory in your home directory, and md5 auth enabled by default, use: 要使用超级用户等于您的用户名,主目录中的数据目录以及默认情况下启用md5 auth来创建新的PostgreSQL实例,请使用:

initdb -D $HOME/my_postgres -A md5 -U $USER

Adjust as desired; 根据需要调整; see initdb --help . 请参阅initdb --help

You'll then need to edit postgresql.conf to change the port to a non-default one, since your system probably runs its own postgres on the default port 5432. (If you want to limit access strictly to you, you can instead set listen_addresses = '' and unix_socket_directories = /home/myuser/postgres_socket or whatever. But it's simpler to just use a different port.) 然后,您需要编辑postgresql.conf以将port更改为非默认port ,因为您的系统可能在默认端口5432上运行自己的postgres。(如果您想严格限制访问权限,则可以改为设置listen_addresses = ''unix_socket_directories = /home/myuser/postgres_socket或者其他什么。但是使用不同的端口更简单。)

To start it: 开始吧:

pg_ctl -D $HOME/my_postgres -w start

To connect to it, specify the port you chose: 要连接到它,请指定您选择的端口:

psql -p 5434 ...

(If you changed unix_socket_directories you'll also want to specify the path you gave, like -h /home/myuser/postgres_socket .) (如果你更改了unix_socket_directories你还需要指定你给出的路径,比如-h /home/myuser/postgres_socket 。)

To make psql etc connect to your postgres by default, edit your ~/.bashrc to add something like 要使psql等默认连接到你的postgres,编辑你的~/.bashrc来添加类似的东西

export PGPORT=5434

but note that'll also affect the default port for connections to other hosts. 但请注意,这也会影响与其他主机连接的默认端口。

To stop it: 要阻止它:

pg_ctl -D $HOME/my_postgres -w stop

but you can also just shut down without stopping it, it doesn't care and will recover safely when you start it next. 但是你也可以在不停止的情况下关闭它,它并不关心,并且当你下次启动时它会安全恢复。

To autostart it when you log in when it's set up in your home directory you'd have to use your desktop environment's run-at-startup features. 要在登录时在主目录中设置时自动启动它,您必须使用桌面环境的run-at-startup功能。 They vary depending on environment and version so I can't give details here; 它们因环境和版本而异,所以我不能在此提供详细信息; it's different for GNOME 3, Unity (ubuntu), KDE, XFCE, etc. 它与GNOME 3,Unity(ubuntu),KDE,XFCE等不同。

Note that this approach still uses the system packages for PostgreSQL. 请注意,此方法仍然使用PostgreSQL的系统 This is important because if you uninstall (say) PostgreSQL 9.4 and install 9.6, your copy in your home dir will stop working. 这很重要,因为如果您卸载(比如说)PostgreSQL 9.4并安装9.6,您家中的副本将停止工作。 If you want it entirely independent of system packages, as you probably do if you don't control the system, you should compile PostgreSQL from source or use the binary installer to install inside your home directory. 如果您希望它完全独立于系统软件包,就像您不控制系统那样,您应该从源代码编译PostgreSQL或使用二进制安装程序安装在您的主目录中。

Postgres can run without root permission. Postgres可以在没有root权限的情况下运行。 Just download from https://www.enterprisedb.com/download-postgresql-binaries 只需从https://www.enterprisedb.com/download-postgresql-binaries下载

and run 并运行

Init database Init数据库

./initdb -D /data

Run postgres 运行postgres

./bin/postgres -D /data

Create database 创建数据库

./bin/createdb mydb

Connect with psql 用psql连接

./bin/psql mydb

( https://www.golery.com/pencil/vU ) https://www.golery.com/pencil/vU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM