PostgreSQL installation error — Cannot allocate memory

£可爱£侵袭症+ 提交于 2019-12-23 06:57:27

问题


I'm trying to switch over from sqlite3 to PostgreSQL for development in Rails so that I don't have any heroku problems. I was following the advice given on heroku and a linked-to Railscast, but I ran into the following error after brew installing postgresql.

creating template1 database in /usr/local/var/postgres/base/1 ...

FATAL: could not create shared memory segment: Cannot allocate memory

DETAIL: Failed system call was shmget(key=1, size=2072576, 03600).

HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space, or exceeded your kernel's SHMALL parameter. You can either reduce the request size or reconfigure the kernel with larger SHMALL. To reduce the request size (currently 2072576 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.

I've poked around the doc a bit, but I'm new to this and know very little about memory and how databases work, and I figured that someone here might be able to point me in the right direction a lot better than I could find it myself. Any idea how to fix this? My computer is new and relatively fancy, and I'd be surprised if it ran out of memory for this, so I don't know if reducing "shared memory usage" is the right idea (if I understand what's going on at all).

Edit: Should have put this up earlier. This is the command (building the database) that led to the error:

initdb /usr/local/var/postgres -E utf8

回答1:


From the database path, I guess you are using Mac OS X.

Since most popular Linux distros has a database directory reside in /var/lib.

After some search on Google, I found this: Fixing the postgresql initdb fatal shared memory error on Leopard

Hope it help.

I copied those instructions from the link above for your convenience.

Run these command:

sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216

Or edit /etc/sysctl.conf for permanent changes

kern.sysv.shmall=65536
kern.sysv.shmmax=16777216 



回答2:


Since the page references in answer does not exist, here is more details on how to set shared memory on Mac.

Ensure that your system is configured to allow the use of larger amounts of shared memory. Note that this does not 'reserve' any memory so it is safe to configure much higher values than you might initially need. You can do this by editing the file /etc/sysctl.conf - e.g.

sudo nano /etc/sysctl.conf

On a MacBook Pro with 2GB of RAM, the example sysctl.conf contains:

kern.sysv.shmmax=1610612736
kern.sysv.shmall=393216
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.maxprocperuid=512
kern.maxproc=2048

Note that (kern.sysv.shmall * 4096) should be greater than or equal to kern.sysv.shmmax. kern.sysv.shmmax must also be a multiple of 4096.

Once you have edited (or created) the file, reboot. If you wish to check the settings currently being used by the kernel, you can use the sysctl utility:

sysctl -a

Enjoy!




回答3:


You must change and reboot:

sudo vim /etc/sysctl.conf

For mac os x with 2 GB

kern.sysv.shmall=393216
kern.sysv.shmmax=1610612736

For mac os x with 4 GB

kern.sysv.shmall=786432
kern.sysv.shmmax=3221225472

For mac os x with 8 GB

kern.sysv.shmall=786432 * 2
kern.sysv.shmmax=3221225472 * 2 

etc ...



来源:https://stackoverflow.com/questions/12433813/postgresql-installation-error-cannot-allocate-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!