Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL installation error -- Cannot allocate memory

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
like image 305
Sasha Avatar asked Sep 15 '12 01:09

Sasha


People also ask

What happens when Postgres runs out of memory?

The most common cause of out of memory issue happens when PostgreSQL is unable to allocate the memory required for a query to run. This is defined by work_mem parameter, which sets the maximum amount of memory that can be used by a query operation before writing to temporary disk files.

How much memory does PostgreSQL use?

The amount of memory consumed by each connection varies based on factors such as the type and count of queries run by the connection, and the usage of temporary tables. As per the test results shown in this post, the memory utilization ranged from around 1.5–14.5 MB per connection.

What is shared memory in PostgreSQL?

Shared Memory: It is allocated by the PostgreSQL server when it is started, and it is used by all the processes. It is divided into sub-areas: Shared buffer pool: Where PostgreSQL loads pages with tables and indexes from disk, to work directly from memory, reducing the disk access.


1 Answers

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 
like image 144
inntran Avatar answered Sep 19 '22 15:09

inntran